fc_lport.c 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright(c) 2007 Intel Corporation. All rights reserved.
  4. *
  5. * Maintained at www.Open-FCoE.org
  6. */
  7. /*
  8. * PORT LOCKING NOTES
  9. *
  10. * These comments only apply to the 'port code' which consists of the lport,
  11. * disc and rport blocks.
  12. *
  13. * MOTIVATION
  14. *
  15. * The lport, disc and rport blocks all have mutexes that are used to protect
  16. * those objects. The main motivation for these locks is to prevent from
  17. * having an lport reset just before we send a frame. In that scenario the
  18. * lport's FID would get set to zero and then we'd send a frame with an
  19. * invalid SID. We also need to ensure that states don't change unexpectedly
  20. * while processing another state.
  21. *
  22. * HIERARCHY
  23. *
  24. * The following hierarchy defines the locking rules. A greater lock
  25. * may be held before acquiring a lesser lock, but a lesser lock should never
  26. * be held while attempting to acquire a greater lock. Here is the hierarchy-
  27. *
  28. * lport > disc, lport > rport, disc > rport
  29. *
  30. * CALLBACKS
  31. *
  32. * The callbacks cause complications with this scheme. There is a callback
  33. * from the rport (to either lport or disc) and a callback from disc
  34. * (to the lport).
  35. *
  36. * As rports exit the rport state machine a callback is made to the owner of
  37. * the rport to notify success or failure. Since the callback is likely to
  38. * cause the lport or disc to grab its lock we cannot hold the rport lock
  39. * while making the callback. To ensure that the rport is not free'd while
  40. * processing the callback the rport callbacks are serialized through a
  41. * single-threaded workqueue. An rport would never be free'd while in a
  42. * callback handler because no other rport work in this queue can be executed
  43. * at the same time.
  44. *
  45. * When discovery succeeds or fails a callback is made to the lport as
  46. * notification. Currently, successful discovery causes the lport to take no
  47. * action. A failure will cause the lport to reset. There is likely a circular
  48. * locking problem with this implementation.
  49. */
  50. /*
  51. * LPORT LOCKING
  52. *
  53. * The critical sections protected by the lport's mutex are quite broad and
  54. * may be improved upon in the future. The lport code and its locking doesn't
  55. * influence the I/O path, so excessive locking doesn't penalize I/O
  56. * performance.
  57. *
  58. * The strategy is to lock whenever processing a request or response. Note
  59. * that every _enter_* function corresponds to a state change. They generally
  60. * change the lports state and then send a request out on the wire. We lock
  61. * before calling any of these functions to protect that state change. This
  62. * means that the entry points into the lport block manage the locks while
  63. * the state machine can transition between states (i.e. _enter_* functions)
  64. * while always staying protected.
  65. *
  66. * When handling responses we also hold the lport mutex broadly. When the
  67. * lport receives the response frame it locks the mutex and then calls the
  68. * appropriate handler for the particuar response. Generally a response will
  69. * trigger a state change and so the lock must already be held.
  70. *
  71. * Retries also have to consider the locking. The retries occur from a work
  72. * context and the work function will lock the lport and then retry the state
  73. * (i.e. _enter_* function).
  74. */
  75. #include <linux/timer.h>
  76. #include <linux/delay.h>
  77. #include <linux/module.h>
  78. #include <linux/slab.h>
  79. #include <asm/unaligned.h>
  80. #include <scsi/fc/fc_gs.h>
  81. #include <scsi/libfc.h>
  82. #include <linux/scatterlist.h>
  83. #include "fc_encode.h"
  84. #include "fc_libfc.h"
  85. /* Fabric IDs to use for point-to-point mode, chosen on whims. */
  86. #define FC_LOCAL_PTP_FID_LO 0x010101
  87. #define FC_LOCAL_PTP_FID_HI 0x010102
  88. #define DNS_DELAY 3 /* Discovery delay after RSCN (in seconds)*/
  89. #define MAX_CT_PAYLOAD 2048
  90. #define DISCOVERED_PORTS 4
  91. #define NUMBER_OF_PORTS 1
  92. static void fc_lport_error(struct fc_lport *, struct fc_frame *);
  93. static void fc_lport_enter_reset(struct fc_lport *);
  94. static void fc_lport_enter_flogi(struct fc_lport *);
  95. static void fc_lport_enter_dns(struct fc_lport *);
  96. static void fc_lport_enter_ns(struct fc_lport *, enum fc_lport_state);
  97. static void fc_lport_enter_scr(struct fc_lport *);
  98. static void fc_lport_enter_ready(struct fc_lport *);
  99. static void fc_lport_enter_logo(struct fc_lport *);
  100. static void fc_lport_enter_fdmi(struct fc_lport *lport);
  101. static void fc_lport_enter_ms(struct fc_lport *, enum fc_lport_state);
  102. static const char *fc_lport_state_names[] = {
  103. [LPORT_ST_DISABLED] = "disabled",
  104. [LPORT_ST_FLOGI] = "FLOGI",
  105. [LPORT_ST_DNS] = "dNS",
  106. [LPORT_ST_RNN_ID] = "RNN_ID",
  107. [LPORT_ST_RSNN_NN] = "RSNN_NN",
  108. [LPORT_ST_RSPN_ID] = "RSPN_ID",
  109. [LPORT_ST_RFT_ID] = "RFT_ID",
  110. [LPORT_ST_RFF_ID] = "RFF_ID",
  111. [LPORT_ST_FDMI] = "FDMI",
  112. [LPORT_ST_RHBA] = "RHBA",
  113. [LPORT_ST_RPA] = "RPA",
  114. [LPORT_ST_DHBA] = "DHBA",
  115. [LPORT_ST_DPRT] = "DPRT",
  116. [LPORT_ST_SCR] = "SCR",
  117. [LPORT_ST_READY] = "Ready",
  118. [LPORT_ST_LOGO] = "LOGO",
  119. [LPORT_ST_RESET] = "reset",
  120. };
  121. /**
  122. * struct fc_bsg_info - FC Passthrough managemet structure
  123. * @job: The passthrough job
  124. * @lport: The local port to pass through a command
  125. * @rsp_code: The expected response code
  126. * @sg: job->reply_payload.sg_list
  127. * @nents: job->reply_payload.sg_cnt
  128. * @offset: The offset into the response data
  129. */
  130. struct fc_bsg_info {
  131. struct bsg_job *job;
  132. struct fc_lport *lport;
  133. u16 rsp_code;
  134. struct scatterlist *sg;
  135. u32 nents;
  136. size_t offset;
  137. };
  138. /**
  139. * fc_frame_drop() - Dummy frame handler
  140. * @lport: The local port the frame was received on
  141. * @fp: The received frame
  142. */
  143. static int fc_frame_drop(struct fc_lport *lport, struct fc_frame *fp)
  144. {
  145. fc_frame_free(fp);
  146. return 0;
  147. }
  148. /**
  149. * fc_lport_rport_callback() - Event handler for rport events
  150. * @lport: The lport which is receiving the event
  151. * @rdata: private remote port data
  152. * @event: The event that occurred
  153. *
  154. * Locking Note: The rport lock should not be held when calling
  155. * this function.
  156. */
  157. static void fc_lport_rport_callback(struct fc_lport *lport,
  158. struct fc_rport_priv *rdata,
  159. enum fc_rport_event event)
  160. {
  161. FC_LPORT_DBG(lport, "Received a %d event for port (%6.6x)\n", event,
  162. rdata->ids.port_id);
  163. mutex_lock(&lport->lp_mutex);
  164. switch (event) {
  165. case RPORT_EV_READY:
  166. if (lport->state == LPORT_ST_DNS) {
  167. lport->dns_rdata = rdata;
  168. fc_lport_enter_ns(lport, LPORT_ST_RNN_ID);
  169. } else if (lport->state == LPORT_ST_FDMI) {
  170. lport->ms_rdata = rdata;
  171. fc_lport_enter_ms(lport, LPORT_ST_DHBA);
  172. } else {
  173. FC_LPORT_DBG(lport, "Received an READY event "
  174. "on port (%6.6x) for the directory "
  175. "server, but the lport is not "
  176. "in the DNS or FDMI state, it's in the "
  177. "%d state", rdata->ids.port_id,
  178. lport->state);
  179. fc_rport_logoff(rdata);
  180. }
  181. break;
  182. case RPORT_EV_LOGO:
  183. case RPORT_EV_FAILED:
  184. case RPORT_EV_STOP:
  185. if (rdata->ids.port_id == FC_FID_DIR_SERV)
  186. lport->dns_rdata = NULL;
  187. else if (rdata->ids.port_id == FC_FID_MGMT_SERV)
  188. lport->ms_rdata = NULL;
  189. break;
  190. case RPORT_EV_NONE:
  191. break;
  192. }
  193. mutex_unlock(&lport->lp_mutex);
  194. }
  195. /**
  196. * fc_lport_state() - Return a string which represents the lport's state
  197. * @lport: The lport whose state is to converted to a string
  198. */
  199. static const char *fc_lport_state(struct fc_lport *lport)
  200. {
  201. const char *cp;
  202. cp = fc_lport_state_names[lport->state];
  203. if (!cp)
  204. cp = "unknown";
  205. return cp;
  206. }
  207. /**
  208. * fc_lport_ptp_setup() - Create an rport for point-to-point mode
  209. * @lport: The lport to attach the ptp rport to
  210. * @remote_fid: The FID of the ptp rport
  211. * @remote_wwpn: The WWPN of the ptp rport
  212. * @remote_wwnn: The WWNN of the ptp rport
  213. */
  214. static void fc_lport_ptp_setup(struct fc_lport *lport,
  215. u32 remote_fid, u64 remote_wwpn,
  216. u64 remote_wwnn)
  217. {
  218. lockdep_assert_held(&lport->lp_mutex);
  219. if (lport->ptp_rdata) {
  220. fc_rport_logoff(lport->ptp_rdata);
  221. kref_put(&lport->ptp_rdata->kref, fc_rport_destroy);
  222. }
  223. mutex_lock(&lport->disc.disc_mutex);
  224. lport->ptp_rdata = fc_rport_create(lport, remote_fid);
  225. if (!lport->ptp_rdata) {
  226. printk(KERN_WARNING "libfc: Failed to setup lport 0x%x\n",
  227. lport->port_id);
  228. mutex_unlock(&lport->disc.disc_mutex);
  229. return;
  230. }
  231. kref_get(&lport->ptp_rdata->kref);
  232. lport->ptp_rdata->ids.port_name = remote_wwpn;
  233. lport->ptp_rdata->ids.node_name = remote_wwnn;
  234. mutex_unlock(&lport->disc.disc_mutex);
  235. fc_rport_login(lport->ptp_rdata);
  236. fc_lport_enter_ready(lport);
  237. }
  238. /**
  239. * fc_get_host_port_state() - Return the port state of the given Scsi_Host
  240. * @shost: The SCSI host whose port state is to be determined
  241. */
  242. void fc_get_host_port_state(struct Scsi_Host *shost)
  243. {
  244. struct fc_lport *lport = shost_priv(shost);
  245. mutex_lock(&lport->lp_mutex);
  246. if (!lport->link_up)
  247. fc_host_port_state(shost) = FC_PORTSTATE_LINKDOWN;
  248. else
  249. switch (lport->state) {
  250. case LPORT_ST_READY:
  251. fc_host_port_state(shost) = FC_PORTSTATE_ONLINE;
  252. break;
  253. default:
  254. fc_host_port_state(shost) = FC_PORTSTATE_OFFLINE;
  255. }
  256. mutex_unlock(&lport->lp_mutex);
  257. }
  258. EXPORT_SYMBOL(fc_get_host_port_state);
  259. /**
  260. * fc_get_host_speed() - Return the speed of the given Scsi_Host
  261. * @shost: The SCSI host whose port speed is to be determined
  262. */
  263. void fc_get_host_speed(struct Scsi_Host *shost)
  264. {
  265. struct fc_lport *lport = shost_priv(shost);
  266. fc_host_speed(shost) = lport->link_speed;
  267. }
  268. EXPORT_SYMBOL(fc_get_host_speed);
  269. /**
  270. * fc_get_host_stats() - Return the Scsi_Host's statistics
  271. * @shost: The SCSI host whose statistics are to be returned
  272. */
  273. struct fc_host_statistics *fc_get_host_stats(struct Scsi_Host *shost)
  274. {
  275. struct fc_host_statistics *fc_stats;
  276. struct fc_lport *lport = shost_priv(shost);
  277. unsigned int cpu;
  278. u64 fcp_in_bytes = 0;
  279. u64 fcp_out_bytes = 0;
  280. fc_stats = &lport->host_stats;
  281. memset(fc_stats, 0, sizeof(struct fc_host_statistics));
  282. fc_stats->seconds_since_last_reset = (jiffies - lport->boot_time) / HZ;
  283. for_each_possible_cpu(cpu) {
  284. struct fc_stats *stats;
  285. stats = per_cpu_ptr(lport->stats, cpu);
  286. fc_stats->tx_frames += READ_ONCE(stats->TxFrames);
  287. fc_stats->tx_words += READ_ONCE(stats->TxWords);
  288. fc_stats->rx_frames += READ_ONCE(stats->RxFrames);
  289. fc_stats->rx_words += READ_ONCE(stats->RxWords);
  290. fc_stats->error_frames += READ_ONCE(stats->ErrorFrames);
  291. fc_stats->invalid_crc_count += READ_ONCE(stats->InvalidCRCCount);
  292. fc_stats->fcp_input_requests += READ_ONCE(stats->InputRequests);
  293. fc_stats->fcp_output_requests += READ_ONCE(stats->OutputRequests);
  294. fc_stats->fcp_control_requests += READ_ONCE(stats->ControlRequests);
  295. fcp_in_bytes += READ_ONCE(stats->InputBytes);
  296. fcp_out_bytes += READ_ONCE(stats->OutputBytes);
  297. fc_stats->fcp_packet_alloc_failures += READ_ONCE(stats->FcpPktAllocFails);
  298. fc_stats->fcp_packet_aborts += READ_ONCE(stats->FcpPktAborts);
  299. fc_stats->fcp_frame_alloc_failures += READ_ONCE(stats->FcpFrameAllocFails);
  300. fc_stats->link_failure_count += READ_ONCE(stats->LinkFailureCount);
  301. }
  302. fc_stats->fcp_input_megabytes = div_u64(fcp_in_bytes, 1000000);
  303. fc_stats->fcp_output_megabytes = div_u64(fcp_out_bytes, 1000000);
  304. fc_stats->lip_count = -1;
  305. fc_stats->nos_count = -1;
  306. fc_stats->loss_of_sync_count = -1;
  307. fc_stats->loss_of_signal_count = -1;
  308. fc_stats->prim_seq_protocol_err_count = -1;
  309. fc_stats->dumped_frames = -1;
  310. /* update exches stats */
  311. fc_exch_update_stats(lport);
  312. return fc_stats;
  313. }
  314. EXPORT_SYMBOL(fc_get_host_stats);
  315. /**
  316. * fc_lport_flogi_fill() - Fill in FLOGI command for request
  317. * @lport: The local port the FLOGI is for
  318. * @flogi: The FLOGI command
  319. * @op: The opcode
  320. */
  321. static void fc_lport_flogi_fill(struct fc_lport *lport,
  322. struct fc_els_flogi *flogi,
  323. unsigned int op)
  324. {
  325. struct fc_els_csp *sp;
  326. struct fc_els_cssp *cp;
  327. memset(flogi, 0, sizeof(*flogi));
  328. flogi->fl_cmd = (u8) op;
  329. put_unaligned_be64(lport->wwpn, &flogi->fl_wwpn);
  330. put_unaligned_be64(lport->wwnn, &flogi->fl_wwnn);
  331. sp = &flogi->fl_csp;
  332. sp->sp_hi_ver = 0x20;
  333. sp->sp_lo_ver = 0x20;
  334. sp->sp_bb_cred = htons(10); /* this gets set by gateway */
  335. sp->sp_bb_data = htons((u16) lport->mfs);
  336. cp = &flogi->fl_cssp[3 - 1]; /* class 3 parameters */
  337. cp->cp_class = htons(FC_CPC_VALID | FC_CPC_SEQ);
  338. if (op != ELS_FLOGI) {
  339. sp->sp_features = htons(FC_SP_FT_CIRO);
  340. sp->sp_tot_seq = htons(255); /* seq. we accept */
  341. sp->sp_rel_off = htons(0x1f);
  342. sp->sp_e_d_tov = htonl(lport->e_d_tov);
  343. cp->cp_rdfs = htons((u16) lport->mfs);
  344. cp->cp_con_seq = htons(255);
  345. cp->cp_open_seq = 1;
  346. }
  347. }
  348. /**
  349. * fc_lport_add_fc4_type() - Add a supported FC-4 type to a local port
  350. * @lport: The local port to add a new FC-4 type to
  351. * @type: The new FC-4 type
  352. */
  353. static void fc_lport_add_fc4_type(struct fc_lport *lport, enum fc_fh_type type)
  354. {
  355. __be32 *mp;
  356. mp = &lport->fcts.ff_type_map[type / FC_NS_BPW];
  357. *mp = htonl(ntohl(*mp) | 1UL << (type % FC_NS_BPW));
  358. }
  359. /**
  360. * fc_lport_recv_rlir_req() - Handle received Registered Link Incident Report.
  361. * @lport: Fibre Channel local port receiving the RLIR
  362. * @fp: The RLIR request frame
  363. */
  364. static void fc_lport_recv_rlir_req(struct fc_lport *lport, struct fc_frame *fp)
  365. {
  366. lockdep_assert_held(&lport->lp_mutex);
  367. FC_LPORT_DBG(lport, "Received RLIR request while in state %s\n",
  368. fc_lport_state(lport));
  369. fc_seq_els_rsp_send(fp, ELS_LS_ACC, NULL);
  370. fc_frame_free(fp);
  371. }
  372. /**
  373. * fc_lport_recv_echo_req() - Handle received ECHO request
  374. * @lport: The local port receiving the ECHO
  375. * @in_fp: ECHO request frame
  376. */
  377. static void fc_lport_recv_echo_req(struct fc_lport *lport,
  378. struct fc_frame *in_fp)
  379. {
  380. struct fc_frame *fp;
  381. unsigned int len;
  382. void *pp;
  383. void *dp;
  384. lockdep_assert_held(&lport->lp_mutex);
  385. FC_LPORT_DBG(lport, "Received ECHO request while in state %s\n",
  386. fc_lport_state(lport));
  387. len = fr_len(in_fp) - sizeof(struct fc_frame_header);
  388. pp = fc_frame_payload_get(in_fp, len);
  389. if (len < sizeof(__be32))
  390. len = sizeof(__be32);
  391. fp = fc_frame_alloc(lport, len);
  392. if (fp) {
  393. dp = fc_frame_payload_get(fp, len);
  394. memcpy(dp, pp, len);
  395. *((__be32 *)dp) = htonl(ELS_LS_ACC << 24);
  396. fc_fill_reply_hdr(fp, in_fp, FC_RCTL_ELS_REP, 0);
  397. lport->tt.frame_send(lport, fp);
  398. }
  399. fc_frame_free(in_fp);
  400. }
  401. /**
  402. * fc_lport_recv_rnid_req() - Handle received Request Node ID data request
  403. * @lport: The local port receiving the RNID
  404. * @in_fp: The RNID request frame
  405. */
  406. static void fc_lport_recv_rnid_req(struct fc_lport *lport,
  407. struct fc_frame *in_fp)
  408. {
  409. struct fc_frame *fp;
  410. struct fc_els_rnid *req;
  411. struct {
  412. struct fc_els_rnid_resp rnid;
  413. struct fc_els_rnid_cid cid;
  414. struct fc_els_rnid_gen gen;
  415. } *rp;
  416. struct fc_seq_els_data rjt_data;
  417. u8 fmt;
  418. size_t len;
  419. lockdep_assert_held(&lport->lp_mutex);
  420. FC_LPORT_DBG(lport, "Received RNID request while in state %s\n",
  421. fc_lport_state(lport));
  422. req = fc_frame_payload_get(in_fp, sizeof(*req));
  423. if (!req) {
  424. rjt_data.reason = ELS_RJT_LOGIC;
  425. rjt_data.explan = ELS_EXPL_NONE;
  426. fc_seq_els_rsp_send(in_fp, ELS_LS_RJT, &rjt_data);
  427. } else {
  428. fmt = req->rnid_fmt;
  429. len = sizeof(*rp);
  430. if (fmt != ELS_RNIDF_GEN ||
  431. ntohl(lport->rnid_gen.rnid_atype) == 0) {
  432. fmt = ELS_RNIDF_NONE; /* nothing to provide */
  433. len -= sizeof(rp->gen);
  434. }
  435. fp = fc_frame_alloc(lport, len);
  436. if (fp) {
  437. rp = fc_frame_payload_get(fp, len);
  438. memset(rp, 0, len);
  439. rp->rnid.rnid_cmd = ELS_LS_ACC;
  440. rp->rnid.rnid_fmt = fmt;
  441. rp->rnid.rnid_cid_len = sizeof(rp->cid);
  442. rp->cid.rnid_wwpn = htonll(lport->wwpn);
  443. rp->cid.rnid_wwnn = htonll(lport->wwnn);
  444. if (fmt == ELS_RNIDF_GEN) {
  445. rp->rnid.rnid_sid_len = sizeof(rp->gen);
  446. memcpy(&rp->gen, &lport->rnid_gen,
  447. sizeof(rp->gen));
  448. }
  449. fc_fill_reply_hdr(fp, in_fp, FC_RCTL_ELS_REP, 0);
  450. lport->tt.frame_send(lport, fp);
  451. }
  452. }
  453. fc_frame_free(in_fp);
  454. }
  455. /**
  456. * fc_lport_recv_logo_req() - Handle received fabric LOGO request
  457. * @lport: The local port receiving the LOGO
  458. * @fp: The LOGO request frame
  459. */
  460. static void fc_lport_recv_logo_req(struct fc_lport *lport, struct fc_frame *fp)
  461. {
  462. lockdep_assert_held(&lport->lp_mutex);
  463. fc_seq_els_rsp_send(fp, ELS_LS_ACC, NULL);
  464. fc_lport_enter_reset(lport);
  465. fc_frame_free(fp);
  466. }
  467. /**
  468. * fc_fabric_login() - Start the lport state machine
  469. * @lport: The local port that should log into the fabric
  470. *
  471. * Locking Note: This function should not be called
  472. * with the lport lock held.
  473. */
  474. int fc_fabric_login(struct fc_lport *lport)
  475. {
  476. int rc = -1;
  477. mutex_lock(&lport->lp_mutex);
  478. if (lport->state == LPORT_ST_DISABLED ||
  479. lport->state == LPORT_ST_LOGO) {
  480. fc_lport_state_enter(lport, LPORT_ST_RESET);
  481. fc_lport_enter_reset(lport);
  482. rc = 0;
  483. }
  484. mutex_unlock(&lport->lp_mutex);
  485. return rc;
  486. }
  487. EXPORT_SYMBOL(fc_fabric_login);
  488. /**
  489. * __fc_linkup() - Handler for transport linkup events
  490. * @lport: The lport whose link is up
  491. */
  492. void __fc_linkup(struct fc_lport *lport)
  493. {
  494. lockdep_assert_held(&lport->lp_mutex);
  495. if (!lport->link_up) {
  496. lport->link_up = 1;
  497. if (lport->state == LPORT_ST_RESET)
  498. fc_lport_enter_flogi(lport);
  499. }
  500. }
  501. /**
  502. * fc_linkup() - Handler for transport linkup events
  503. * @lport: The local port whose link is up
  504. */
  505. void fc_linkup(struct fc_lport *lport)
  506. {
  507. printk(KERN_INFO "host%d: libfc: Link up on port (%6.6x)\n",
  508. lport->host->host_no, lport->port_id);
  509. mutex_lock(&lport->lp_mutex);
  510. __fc_linkup(lport);
  511. mutex_unlock(&lport->lp_mutex);
  512. }
  513. EXPORT_SYMBOL(fc_linkup);
  514. /**
  515. * __fc_linkdown() - Handler for transport linkdown events
  516. * @lport: The lport whose link is down
  517. */
  518. void __fc_linkdown(struct fc_lport *lport)
  519. {
  520. lockdep_assert_held(&lport->lp_mutex);
  521. if (lport->link_up) {
  522. lport->link_up = 0;
  523. fc_lport_enter_reset(lport);
  524. lport->tt.fcp_cleanup(lport);
  525. }
  526. }
  527. /**
  528. * fc_linkdown() - Handler for transport linkdown events
  529. * @lport: The local port whose link is down
  530. */
  531. void fc_linkdown(struct fc_lport *lport)
  532. {
  533. printk(KERN_INFO "host%d: libfc: Link down on port (%6.6x)\n",
  534. lport->host->host_no, lport->port_id);
  535. mutex_lock(&lport->lp_mutex);
  536. __fc_linkdown(lport);
  537. mutex_unlock(&lport->lp_mutex);
  538. }
  539. EXPORT_SYMBOL(fc_linkdown);
  540. /**
  541. * fc_fabric_logoff() - Logout of the fabric
  542. * @lport: The local port to logoff the fabric
  543. *
  544. * Return value:
  545. * 0 for success, -1 for failure
  546. */
  547. int fc_fabric_logoff(struct fc_lport *lport)
  548. {
  549. lport->tt.disc_stop_final(lport);
  550. mutex_lock(&lport->lp_mutex);
  551. if (lport->dns_rdata)
  552. fc_rport_logoff(lport->dns_rdata);
  553. mutex_unlock(&lport->lp_mutex);
  554. fc_rport_flush_queue();
  555. mutex_lock(&lport->lp_mutex);
  556. fc_lport_enter_logo(lport);
  557. mutex_unlock(&lport->lp_mutex);
  558. cancel_delayed_work_sync(&lport->retry_work);
  559. return 0;
  560. }
  561. EXPORT_SYMBOL(fc_fabric_logoff);
  562. /**
  563. * fc_lport_destroy() - Unregister a fc_lport
  564. * @lport: The local port to unregister
  565. *
  566. * Note:
  567. * exit routine for fc_lport instance
  568. * clean-up all the allocated memory
  569. * and free up other system resources.
  570. *
  571. */
  572. int fc_lport_destroy(struct fc_lport *lport)
  573. {
  574. mutex_lock(&lport->lp_mutex);
  575. lport->state = LPORT_ST_DISABLED;
  576. lport->link_up = 0;
  577. lport->tt.frame_send = fc_frame_drop;
  578. mutex_unlock(&lport->lp_mutex);
  579. lport->tt.fcp_abort_io(lport);
  580. lport->tt.disc_stop_final(lport);
  581. lport->tt.exch_mgr_reset(lport, 0, 0);
  582. cancel_delayed_work_sync(&lport->retry_work);
  583. fc_fc4_del_lport(lport);
  584. return 0;
  585. }
  586. EXPORT_SYMBOL(fc_lport_destroy);
  587. /**
  588. * fc_set_mfs() - Set the maximum frame size for a local port
  589. * @lport: The local port to set the MFS for
  590. * @mfs: The new MFS
  591. */
  592. int fc_set_mfs(struct fc_lport *lport, u32 mfs)
  593. {
  594. unsigned int old_mfs;
  595. int rc = -EINVAL;
  596. mutex_lock(&lport->lp_mutex);
  597. old_mfs = lport->mfs;
  598. if (mfs >= FC_MIN_MAX_FRAME) {
  599. mfs &= ~3;
  600. if (mfs > FC_MAX_FRAME)
  601. mfs = FC_MAX_FRAME;
  602. mfs -= sizeof(struct fc_frame_header);
  603. lport->mfs = mfs;
  604. rc = 0;
  605. }
  606. if (!rc && mfs < old_mfs)
  607. fc_lport_enter_reset(lport);
  608. mutex_unlock(&lport->lp_mutex);
  609. return rc;
  610. }
  611. EXPORT_SYMBOL(fc_set_mfs);
  612. /**
  613. * fc_lport_disc_callback() - Callback for discovery events
  614. * @lport: The local port receiving the event
  615. * @event: The discovery event
  616. */
  617. static void fc_lport_disc_callback(struct fc_lport *lport,
  618. enum fc_disc_event event)
  619. {
  620. switch (event) {
  621. case DISC_EV_SUCCESS:
  622. FC_LPORT_DBG(lport, "Discovery succeeded\n");
  623. break;
  624. case DISC_EV_FAILED:
  625. printk(KERN_ERR "host%d: libfc: "
  626. "Discovery failed for port (%6.6x)\n",
  627. lport->host->host_no, lport->port_id);
  628. mutex_lock(&lport->lp_mutex);
  629. fc_lport_enter_reset(lport);
  630. mutex_unlock(&lport->lp_mutex);
  631. break;
  632. case DISC_EV_NONE:
  633. WARN_ON(1);
  634. break;
  635. }
  636. }
  637. /**
  638. * fc_lport_enter_ready() - Enter the ready state and start discovery
  639. * @lport: The local port that is ready
  640. */
  641. static void fc_lport_enter_ready(struct fc_lport *lport)
  642. {
  643. lockdep_assert_held(&lport->lp_mutex);
  644. FC_LPORT_DBG(lport, "Entered READY from state %s\n",
  645. fc_lport_state(lport));
  646. fc_lport_state_enter(lport, LPORT_ST_READY);
  647. if (lport->vport)
  648. fc_vport_set_state(lport->vport, FC_VPORT_ACTIVE);
  649. fc_vports_linkchange(lport);
  650. if (!lport->ptp_rdata)
  651. lport->tt.disc_start(fc_lport_disc_callback, lport);
  652. }
  653. /**
  654. * fc_lport_set_port_id() - set the local port Port ID
  655. * @lport: The local port which will have its Port ID set.
  656. * @port_id: The new port ID.
  657. * @fp: The frame containing the incoming request, or NULL.
  658. */
  659. static void fc_lport_set_port_id(struct fc_lport *lport, u32 port_id,
  660. struct fc_frame *fp)
  661. {
  662. lockdep_assert_held(&lport->lp_mutex);
  663. if (port_id)
  664. printk(KERN_INFO "host%d: Assigned Port ID %6.6x\n",
  665. lport->host->host_no, port_id);
  666. lport->port_id = port_id;
  667. /* Update the fc_host */
  668. fc_host_port_id(lport->host) = port_id;
  669. if (lport->tt.lport_set_port_id)
  670. lport->tt.lport_set_port_id(lport, port_id, fp);
  671. }
  672. /**
  673. * fc_lport_set_local_id() - set the local port Port ID for point-to-multipoint
  674. * @lport: The local port which will have its Port ID set.
  675. * @port_id: The new port ID.
  676. *
  677. * Called by the lower-level driver when transport sets the local port_id.
  678. * This is used in VN_port to VN_port mode for FCoE, and causes FLOGI and
  679. * discovery to be skipped.
  680. */
  681. void fc_lport_set_local_id(struct fc_lport *lport, u32 port_id)
  682. {
  683. mutex_lock(&lport->lp_mutex);
  684. fc_lport_set_port_id(lport, port_id, NULL);
  685. switch (lport->state) {
  686. case LPORT_ST_RESET:
  687. case LPORT_ST_FLOGI:
  688. if (port_id)
  689. fc_lport_enter_ready(lport);
  690. break;
  691. default:
  692. break;
  693. }
  694. mutex_unlock(&lport->lp_mutex);
  695. }
  696. EXPORT_SYMBOL(fc_lport_set_local_id);
  697. /**
  698. * fc_lport_recv_flogi_req() - Receive a FLOGI request
  699. * @lport: The local port that received the request
  700. * @rx_fp: The FLOGI frame
  701. *
  702. * A received FLOGI request indicates a point-to-point connection.
  703. * Accept it with the common service parameters indicating our N port.
  704. * Set up to do a PLOGI if we have the higher-number WWPN.
  705. */
  706. static void fc_lport_recv_flogi_req(struct fc_lport *lport,
  707. struct fc_frame *rx_fp)
  708. {
  709. struct fc_frame *fp;
  710. struct fc_frame_header *fh;
  711. struct fc_els_flogi *flp;
  712. struct fc_els_flogi *new_flp;
  713. u64 remote_wwpn;
  714. u32 remote_fid;
  715. u32 local_fid;
  716. lockdep_assert_held(&lport->lp_mutex);
  717. FC_LPORT_DBG(lport, "Received FLOGI request while in state %s\n",
  718. fc_lport_state(lport));
  719. remote_fid = fc_frame_sid(rx_fp);
  720. flp = fc_frame_payload_get(rx_fp, sizeof(*flp));
  721. if (!flp)
  722. goto out;
  723. remote_wwpn = get_unaligned_be64(&flp->fl_wwpn);
  724. if (remote_wwpn == lport->wwpn) {
  725. printk(KERN_WARNING "host%d: libfc: Received FLOGI from port "
  726. "with same WWPN %16.16llx\n",
  727. lport->host->host_no, remote_wwpn);
  728. goto out;
  729. }
  730. FC_LPORT_DBG(lport, "FLOGI from port WWPN %16.16llx\n", remote_wwpn);
  731. /*
  732. * XXX what is the right thing to do for FIDs?
  733. * The originator might expect our S_ID to be 0xfffffe.
  734. * But if so, both of us could end up with the same FID.
  735. */
  736. local_fid = FC_LOCAL_PTP_FID_LO;
  737. if (remote_wwpn < lport->wwpn) {
  738. local_fid = FC_LOCAL_PTP_FID_HI;
  739. if (!remote_fid || remote_fid == local_fid)
  740. remote_fid = FC_LOCAL_PTP_FID_LO;
  741. } else if (!remote_fid) {
  742. remote_fid = FC_LOCAL_PTP_FID_HI;
  743. }
  744. fc_lport_set_port_id(lport, local_fid, rx_fp);
  745. fp = fc_frame_alloc(lport, sizeof(*flp));
  746. if (fp) {
  747. new_flp = fc_frame_payload_get(fp, sizeof(*flp));
  748. fc_lport_flogi_fill(lport, new_flp, ELS_FLOGI);
  749. new_flp->fl_cmd = (u8) ELS_LS_ACC;
  750. /*
  751. * Send the response. If this fails, the originator should
  752. * repeat the sequence.
  753. */
  754. fc_fill_reply_hdr(fp, rx_fp, FC_RCTL_ELS_REP, 0);
  755. fh = fc_frame_header_get(fp);
  756. hton24(fh->fh_s_id, local_fid);
  757. hton24(fh->fh_d_id, remote_fid);
  758. lport->tt.frame_send(lport, fp);
  759. } else {
  760. fc_lport_error(lport, fp);
  761. }
  762. fc_lport_ptp_setup(lport, remote_fid, remote_wwpn,
  763. get_unaligned_be64(&flp->fl_wwnn));
  764. out:
  765. fc_frame_free(rx_fp);
  766. }
  767. /**
  768. * fc_lport_recv_els_req() - The generic lport ELS request handler
  769. * @lport: The local port that received the request
  770. * @fp: The request frame
  771. *
  772. * This function will see if the lport handles the request or
  773. * if an rport should handle the request.
  774. *
  775. * Locking Note: This function should not be called with the lport
  776. * lock held because it will grab the lock.
  777. */
  778. static void fc_lport_recv_els_req(struct fc_lport *lport,
  779. struct fc_frame *fp)
  780. {
  781. mutex_lock(&lport->lp_mutex);
  782. /*
  783. * Handle special ELS cases like FLOGI, LOGO, and
  784. * RSCN here. These don't require a session.
  785. * Even if we had a session, it might not be ready.
  786. */
  787. if (!lport->link_up)
  788. fc_frame_free(fp);
  789. else {
  790. /*
  791. * Check opcode.
  792. */
  793. switch (fc_frame_payload_op(fp)) {
  794. case ELS_FLOGI:
  795. if (!lport->point_to_multipoint)
  796. fc_lport_recv_flogi_req(lport, fp);
  797. else
  798. fc_rport_recv_req(lport, fp);
  799. break;
  800. case ELS_LOGO:
  801. if (fc_frame_sid(fp) == FC_FID_FLOGI)
  802. fc_lport_recv_logo_req(lport, fp);
  803. else
  804. fc_rport_recv_req(lport, fp);
  805. break;
  806. case ELS_RSCN:
  807. lport->tt.disc_recv_req(lport, fp);
  808. break;
  809. case ELS_ECHO:
  810. fc_lport_recv_echo_req(lport, fp);
  811. break;
  812. case ELS_RLIR:
  813. fc_lport_recv_rlir_req(lport, fp);
  814. break;
  815. case ELS_RNID:
  816. fc_lport_recv_rnid_req(lport, fp);
  817. break;
  818. default:
  819. fc_rport_recv_req(lport, fp);
  820. break;
  821. }
  822. }
  823. mutex_unlock(&lport->lp_mutex);
  824. }
  825. static int fc_lport_els_prli(struct fc_rport_priv *rdata, u32 spp_len,
  826. const struct fc_els_spp *spp_in,
  827. struct fc_els_spp *spp_out)
  828. {
  829. return FC_SPP_RESP_INVL;
  830. }
  831. struct fc4_prov fc_lport_els_prov = {
  832. .prli = fc_lport_els_prli,
  833. .recv = fc_lport_recv_els_req,
  834. };
  835. /**
  836. * fc_lport_recv() - The generic lport request handler
  837. * @lport: The lport that received the request
  838. * @fp: The frame the request is in
  839. *
  840. * Locking Note: This function should not be called with the lport
  841. * lock held because it may grab the lock.
  842. */
  843. void fc_lport_recv(struct fc_lport *lport, struct fc_frame *fp)
  844. {
  845. struct fc_frame_header *fh = fc_frame_header_get(fp);
  846. struct fc_seq *sp = fr_seq(fp);
  847. struct fc4_prov *prov;
  848. /*
  849. * Use RCU read lock and module_lock to be sure module doesn't
  850. * deregister and get unloaded while we're calling it.
  851. * try_module_get() is inlined and accepts a NULL parameter.
  852. * Only ELSes and FCP target ops should come through here.
  853. * The locking is unfortunate, and a better scheme is being sought.
  854. */
  855. rcu_read_lock();
  856. if (fh->fh_type >= FC_FC4_PROV_SIZE)
  857. goto drop;
  858. prov = rcu_dereference(fc_passive_prov[fh->fh_type]);
  859. if (!prov || !try_module_get(prov->module))
  860. goto drop;
  861. rcu_read_unlock();
  862. prov->recv(lport, fp);
  863. module_put(prov->module);
  864. return;
  865. drop:
  866. rcu_read_unlock();
  867. FC_LPORT_DBG(lport, "dropping unexpected frame type %x\n", fh->fh_type);
  868. fc_frame_free(fp);
  869. if (sp)
  870. fc_exch_done(sp);
  871. }
  872. EXPORT_SYMBOL(fc_lport_recv);
  873. /**
  874. * fc_lport_reset() - Reset a local port
  875. * @lport: The local port which should be reset
  876. *
  877. * Locking Note: This functions should not be called with the
  878. * lport lock held.
  879. */
  880. int fc_lport_reset(struct fc_lport *lport)
  881. {
  882. cancel_delayed_work_sync(&lport->retry_work);
  883. mutex_lock(&lport->lp_mutex);
  884. fc_lport_enter_reset(lport);
  885. mutex_unlock(&lport->lp_mutex);
  886. return 0;
  887. }
  888. EXPORT_SYMBOL(fc_lport_reset);
  889. /**
  890. * fc_lport_reset_locked() - Reset the local port w/ the lport lock held
  891. * @lport: The local port to be reset
  892. */
  893. static void fc_lport_reset_locked(struct fc_lport *lport)
  894. {
  895. lockdep_assert_held(&lport->lp_mutex);
  896. if (lport->dns_rdata) {
  897. fc_rport_logoff(lport->dns_rdata);
  898. lport->dns_rdata = NULL;
  899. }
  900. if (lport->ptp_rdata) {
  901. fc_rport_logoff(lport->ptp_rdata);
  902. kref_put(&lport->ptp_rdata->kref, fc_rport_destroy);
  903. lport->ptp_rdata = NULL;
  904. }
  905. lport->tt.disc_stop(lport);
  906. lport->tt.exch_mgr_reset(lport, 0, 0);
  907. fc_host_fabric_name(lport->host) = 0;
  908. if (lport->port_id && (!lport->point_to_multipoint || !lport->link_up))
  909. fc_lport_set_port_id(lport, 0, NULL);
  910. }
  911. /**
  912. * fc_lport_enter_reset() - Reset the local port
  913. * @lport: The local port to be reset
  914. */
  915. static void fc_lport_enter_reset(struct fc_lport *lport)
  916. {
  917. lockdep_assert_held(&lport->lp_mutex);
  918. FC_LPORT_DBG(lport, "Entered RESET state from %s state\n",
  919. fc_lport_state(lport));
  920. if (lport->state == LPORT_ST_DISABLED || lport->state == LPORT_ST_LOGO)
  921. return;
  922. if (lport->vport) {
  923. if (lport->link_up)
  924. fc_vport_set_state(lport->vport, FC_VPORT_INITIALIZING);
  925. else
  926. fc_vport_set_state(lport->vport, FC_VPORT_LINKDOWN);
  927. }
  928. fc_lport_state_enter(lport, LPORT_ST_RESET);
  929. fc_host_post_event(lport->host, fc_get_event_number(),
  930. FCH_EVT_LIPRESET, 0);
  931. fc_vports_linkchange(lport);
  932. fc_lport_reset_locked(lport);
  933. if (lport->link_up)
  934. fc_lport_enter_flogi(lport);
  935. }
  936. /**
  937. * fc_lport_enter_disabled() - Disable the local port
  938. * @lport: The local port to be reset
  939. */
  940. static void fc_lport_enter_disabled(struct fc_lport *lport)
  941. {
  942. lockdep_assert_held(&lport->lp_mutex);
  943. FC_LPORT_DBG(lport, "Entered disabled state from %s state\n",
  944. fc_lport_state(lport));
  945. fc_lport_state_enter(lport, LPORT_ST_DISABLED);
  946. fc_vports_linkchange(lport);
  947. fc_lport_reset_locked(lport);
  948. }
  949. /**
  950. * fc_lport_error() - Handler for any errors
  951. * @lport: The local port that the error was on
  952. * @fp: The error code encoded in a frame pointer
  953. *
  954. * If the error was caused by a resource allocation failure
  955. * then wait for half a second and retry, otherwise retry
  956. * after the e_d_tov time.
  957. */
  958. static void fc_lport_error(struct fc_lport *lport, struct fc_frame *fp)
  959. {
  960. unsigned long delay = 0;
  961. FC_LPORT_DBG(lport, "Error %ld in state %s, retries %d\n",
  962. IS_ERR(fp) ? -PTR_ERR(fp) : 0, fc_lport_state(lport),
  963. lport->retry_count);
  964. if (PTR_ERR(fp) == -FC_EX_CLOSED)
  965. return;
  966. /*
  967. * Memory allocation failure, or the exchange timed out
  968. * or we received LS_RJT.
  969. * Retry after delay
  970. */
  971. if (lport->retry_count < lport->max_retry_count) {
  972. lport->retry_count++;
  973. if (!fp)
  974. delay = msecs_to_jiffies(500);
  975. else
  976. delay = msecs_to_jiffies(lport->e_d_tov);
  977. schedule_delayed_work(&lport->retry_work, delay);
  978. } else
  979. fc_lport_enter_reset(lport);
  980. }
  981. /**
  982. * fc_lport_ns_resp() - Handle response to a name server
  983. * registration exchange
  984. * @sp: current sequence in exchange
  985. * @fp: response frame
  986. * @lp_arg: Fibre Channel host port instance
  987. *
  988. * Locking Note: This function will be called without the lport lock
  989. * held, but it will lock, call an _enter_* function or fc_lport_error()
  990. * and then unlock the lport.
  991. */
  992. static void fc_lport_ns_resp(struct fc_seq *sp, struct fc_frame *fp,
  993. void *lp_arg)
  994. {
  995. struct fc_lport *lport = lp_arg;
  996. struct fc_frame_header *fh;
  997. struct fc_ct_hdr *ct;
  998. FC_LPORT_DBG(lport, "Received a ns %s\n", fc_els_resp_type(fp));
  999. if (fp == ERR_PTR(-FC_EX_CLOSED))
  1000. return;
  1001. mutex_lock(&lport->lp_mutex);
  1002. if (lport->state < LPORT_ST_RNN_ID || lport->state > LPORT_ST_RFF_ID) {
  1003. FC_LPORT_DBG(lport, "Received a name server response, "
  1004. "but in state %s\n", fc_lport_state(lport));
  1005. if (IS_ERR(fp))
  1006. goto err;
  1007. goto out;
  1008. }
  1009. if (IS_ERR(fp)) {
  1010. fc_lport_error(lport, fp);
  1011. goto err;
  1012. }
  1013. fh = fc_frame_header_get(fp);
  1014. ct = fc_frame_payload_get(fp, sizeof(*ct));
  1015. if (fh && ct && fh->fh_type == FC_TYPE_CT &&
  1016. ct->ct_fs_type == FC_FST_DIR &&
  1017. ct->ct_fs_subtype == FC_NS_SUBTYPE &&
  1018. ntohs(ct->ct_cmd) == FC_FS_ACC)
  1019. switch (lport->state) {
  1020. case LPORT_ST_RNN_ID:
  1021. fc_lport_enter_ns(lport, LPORT_ST_RSNN_NN);
  1022. break;
  1023. case LPORT_ST_RSNN_NN:
  1024. fc_lport_enter_ns(lport, LPORT_ST_RSPN_ID);
  1025. break;
  1026. case LPORT_ST_RSPN_ID:
  1027. fc_lport_enter_ns(lport, LPORT_ST_RFT_ID);
  1028. break;
  1029. case LPORT_ST_RFT_ID:
  1030. fc_lport_enter_ns(lport, LPORT_ST_RFF_ID);
  1031. break;
  1032. case LPORT_ST_RFF_ID:
  1033. if (lport->fdmi_enabled)
  1034. fc_lport_enter_fdmi(lport);
  1035. else
  1036. fc_lport_enter_scr(lport);
  1037. break;
  1038. default:
  1039. /* should have already been caught by state checks */
  1040. break;
  1041. }
  1042. else
  1043. fc_lport_error(lport, fp);
  1044. out:
  1045. fc_frame_free(fp);
  1046. err:
  1047. mutex_unlock(&lport->lp_mutex);
  1048. }
  1049. /**
  1050. * fc_lport_ms_resp() - Handle response to a management server
  1051. * exchange
  1052. * @sp: current sequence in exchange
  1053. * @fp: response frame
  1054. * @lp_arg: Fibre Channel host port instance
  1055. *
  1056. * Locking Note: This function will be called without the lport lock
  1057. * held, but it will lock, call an _enter_* function or fc_lport_error()
  1058. * and then unlock the lport.
  1059. */
  1060. static void fc_lport_ms_resp(struct fc_seq *sp, struct fc_frame *fp,
  1061. void *lp_arg)
  1062. {
  1063. struct fc_lport *lport = lp_arg;
  1064. struct fc_frame_header *fh;
  1065. struct fc_ct_hdr *ct;
  1066. struct fc_host_attrs *fc_host = shost_to_fc_host(lport->host);
  1067. FC_LPORT_DBG(lport, "Received a ms %s\n", fc_els_resp_type(fp));
  1068. if (fp == ERR_PTR(-FC_EX_CLOSED))
  1069. return;
  1070. mutex_lock(&lport->lp_mutex);
  1071. if (lport->state < LPORT_ST_RHBA || lport->state > LPORT_ST_DPRT) {
  1072. FC_LPORT_DBG(lport, "Received a management server response, "
  1073. "but in state %s\n", fc_lport_state(lport));
  1074. if (IS_ERR(fp))
  1075. goto err;
  1076. goto out;
  1077. }
  1078. if (IS_ERR(fp)) {
  1079. fc_lport_error(lport, fp);
  1080. goto err;
  1081. }
  1082. fh = fc_frame_header_get(fp);
  1083. ct = fc_frame_payload_get(fp, sizeof(*ct));
  1084. if (fh && ct && fh->fh_type == FC_TYPE_CT &&
  1085. ct->ct_fs_type == FC_FST_MGMT &&
  1086. ct->ct_fs_subtype == FC_FDMI_SUBTYPE) {
  1087. FC_LPORT_DBG(lport, "Received a management server response, "
  1088. "reason=%d explain=%d\n",
  1089. ct->ct_reason,
  1090. ct->ct_explan);
  1091. switch (lport->state) {
  1092. case LPORT_ST_RHBA:
  1093. if ((ntohs(ct->ct_cmd) == FC_FS_RJT) && fc_host->fdmi_version == FDMI_V2) {
  1094. FC_LPORT_DBG(lport, "Error for FDMI-V2, fall back to FDMI-V1\n");
  1095. fc_host->fdmi_version = FDMI_V1;
  1096. fc_lport_enter_ms(lport, LPORT_ST_RHBA);
  1097. } else if (ntohs(ct->ct_cmd) == FC_FS_ACC)
  1098. fc_lport_enter_ms(lport, LPORT_ST_RPA);
  1099. else /* Error Skip RPA */
  1100. fc_lport_enter_scr(lport);
  1101. break;
  1102. case LPORT_ST_RPA:
  1103. fc_lport_enter_scr(lport);
  1104. break;
  1105. case LPORT_ST_DPRT:
  1106. fc_lport_enter_ms(lport, LPORT_ST_RHBA);
  1107. break;
  1108. case LPORT_ST_DHBA:
  1109. fc_lport_enter_ms(lport, LPORT_ST_DPRT);
  1110. break;
  1111. default:
  1112. /* should have already been caught by state checks */
  1113. break;
  1114. }
  1115. } else {
  1116. /* Invalid Frame? */
  1117. fc_lport_error(lport, fp);
  1118. }
  1119. out:
  1120. fc_frame_free(fp);
  1121. err:
  1122. mutex_unlock(&lport->lp_mutex);
  1123. }
  1124. /**
  1125. * fc_lport_scr_resp() - Handle response to State Change Register (SCR) request
  1126. * @sp: current sequence in SCR exchange
  1127. * @fp: response frame
  1128. * @lp_arg: Fibre Channel lport port instance that sent the registration request
  1129. *
  1130. * Locking Note: This function will be called without the lport lock
  1131. * held, but it will lock, call an _enter_* function or fc_lport_error
  1132. * and then unlock the lport.
  1133. */
  1134. static void fc_lport_scr_resp(struct fc_seq *sp, struct fc_frame *fp,
  1135. void *lp_arg)
  1136. {
  1137. struct fc_lport *lport = lp_arg;
  1138. u8 op;
  1139. FC_LPORT_DBG(lport, "Received a SCR %s\n", fc_els_resp_type(fp));
  1140. if (fp == ERR_PTR(-FC_EX_CLOSED))
  1141. return;
  1142. mutex_lock(&lport->lp_mutex);
  1143. if (lport->state != LPORT_ST_SCR) {
  1144. FC_LPORT_DBG(lport, "Received a SCR response, but in state "
  1145. "%s\n", fc_lport_state(lport));
  1146. if (IS_ERR(fp))
  1147. goto err;
  1148. goto out;
  1149. }
  1150. if (IS_ERR(fp)) {
  1151. fc_lport_error(lport, fp);
  1152. goto err;
  1153. }
  1154. op = fc_frame_payload_op(fp);
  1155. if (op == ELS_LS_ACC)
  1156. fc_lport_enter_ready(lport);
  1157. else
  1158. fc_lport_error(lport, fp);
  1159. out:
  1160. fc_frame_free(fp);
  1161. err:
  1162. mutex_unlock(&lport->lp_mutex);
  1163. }
  1164. /**
  1165. * fc_lport_enter_scr() - Send a SCR (State Change Register) request
  1166. * @lport: The local port to register for state changes
  1167. */
  1168. static void fc_lport_enter_scr(struct fc_lport *lport)
  1169. {
  1170. struct fc_frame *fp;
  1171. lockdep_assert_held(&lport->lp_mutex);
  1172. FC_LPORT_DBG(lport, "Entered SCR state from %s state\n",
  1173. fc_lport_state(lport));
  1174. fc_lport_state_enter(lport, LPORT_ST_SCR);
  1175. fp = fc_frame_alloc(lport, sizeof(struct fc_els_scr));
  1176. if (!fp) {
  1177. fc_lport_error(lport, fp);
  1178. return;
  1179. }
  1180. if (!lport->tt.elsct_send(lport, FC_FID_FCTRL, fp, ELS_SCR,
  1181. fc_lport_scr_resp, lport,
  1182. 2 * lport->r_a_tov))
  1183. fc_lport_error(lport, NULL);
  1184. }
  1185. /**
  1186. * fc_lport_enter_ns() - register some object with the name server
  1187. * @lport: Fibre Channel local port to register
  1188. * @state: Local port state
  1189. */
  1190. static void fc_lport_enter_ns(struct fc_lport *lport, enum fc_lport_state state)
  1191. {
  1192. struct fc_frame *fp;
  1193. enum fc_ns_req cmd;
  1194. int size = sizeof(struct fc_ct_hdr);
  1195. size_t len;
  1196. lockdep_assert_held(&lport->lp_mutex);
  1197. FC_LPORT_DBG(lport, "Entered %s state from %s state\n",
  1198. fc_lport_state_names[state],
  1199. fc_lport_state(lport));
  1200. fc_lport_state_enter(lport, state);
  1201. switch (state) {
  1202. case LPORT_ST_RNN_ID:
  1203. cmd = FC_NS_RNN_ID;
  1204. size += sizeof(struct fc_ns_rn_id);
  1205. break;
  1206. case LPORT_ST_RSNN_NN:
  1207. len = strnlen(fc_host_symbolic_name(lport->host), 255);
  1208. /* if there is no symbolic name, skip to RFT_ID */
  1209. if (!len)
  1210. return fc_lport_enter_ns(lport, LPORT_ST_RFT_ID);
  1211. cmd = FC_NS_RSNN_NN;
  1212. size += sizeof(struct fc_ns_rsnn) + len;
  1213. break;
  1214. case LPORT_ST_RSPN_ID:
  1215. len = strnlen(fc_host_symbolic_name(lport->host), 255);
  1216. /* if there is no symbolic name, skip to RFT_ID */
  1217. if (!len)
  1218. return fc_lport_enter_ns(lport, LPORT_ST_RFT_ID);
  1219. cmd = FC_NS_RSPN_ID;
  1220. size += sizeof(struct fc_ns_rspn) + len;
  1221. break;
  1222. case LPORT_ST_RFT_ID:
  1223. cmd = FC_NS_RFT_ID;
  1224. size += sizeof(struct fc_ns_rft);
  1225. break;
  1226. case LPORT_ST_RFF_ID:
  1227. cmd = FC_NS_RFF_ID;
  1228. size += sizeof(struct fc_ns_rff_id);
  1229. break;
  1230. default:
  1231. fc_lport_error(lport, NULL);
  1232. return;
  1233. }
  1234. fp = fc_frame_alloc(lport, size);
  1235. if (!fp) {
  1236. fc_lport_error(lport, fp);
  1237. return;
  1238. }
  1239. if (!lport->tt.elsct_send(lport, FC_FID_DIR_SERV, fp, cmd,
  1240. fc_lport_ns_resp,
  1241. lport, 3 * lport->r_a_tov))
  1242. fc_lport_error(lport, fp);
  1243. }
  1244. static struct fc_rport_operations fc_lport_rport_ops = {
  1245. .event_callback = fc_lport_rport_callback,
  1246. };
  1247. /**
  1248. * fc_lport_enter_dns() - Create a fc_rport for the name server
  1249. * @lport: The local port requesting a remote port for the name server
  1250. */
  1251. static void fc_lport_enter_dns(struct fc_lport *lport)
  1252. {
  1253. struct fc_rport_priv *rdata;
  1254. lockdep_assert_held(&lport->lp_mutex);
  1255. FC_LPORT_DBG(lport, "Entered DNS state from %s state\n",
  1256. fc_lport_state(lport));
  1257. fc_lport_state_enter(lport, LPORT_ST_DNS);
  1258. mutex_lock(&lport->disc.disc_mutex);
  1259. rdata = fc_rport_create(lport, FC_FID_DIR_SERV);
  1260. mutex_unlock(&lport->disc.disc_mutex);
  1261. if (!rdata)
  1262. goto err;
  1263. rdata->ops = &fc_lport_rport_ops;
  1264. fc_rport_login(rdata);
  1265. return;
  1266. err:
  1267. fc_lport_error(lport, NULL);
  1268. }
  1269. /**
  1270. * fc_lport_enter_ms() - management server commands
  1271. * @lport: Fibre Channel local port to register
  1272. * @state: Local port state
  1273. */
  1274. static void fc_lport_enter_ms(struct fc_lport *lport, enum fc_lport_state state)
  1275. {
  1276. struct fc_frame *fp;
  1277. enum fc_fdmi_req cmd;
  1278. int size = sizeof(struct fc_ct_hdr);
  1279. size_t len;
  1280. int numattrs;
  1281. struct fc_host_attrs *fc_host = shost_to_fc_host(lport->host);
  1282. lockdep_assert_held(&lport->lp_mutex);
  1283. FC_LPORT_DBG(lport, "Entered %s state from %s state\n",
  1284. fc_lport_state_names[state],
  1285. fc_lport_state(lport));
  1286. fc_lport_state_enter(lport, state);
  1287. switch (state) {
  1288. case LPORT_ST_RHBA:
  1289. cmd = FC_FDMI_RHBA;
  1290. /* Number of HBA Attributes */
  1291. numattrs = 11;
  1292. len = sizeof(struct fc_fdmi_rhba);
  1293. len -= sizeof(struct fc_fdmi_attr_entry);
  1294. len += FC_FDMI_HBA_ATTR_NODENAME_LEN;
  1295. len += FC_FDMI_HBA_ATTR_MANUFACTURER_LEN;
  1296. len += FC_FDMI_HBA_ATTR_SERIALNUMBER_LEN;
  1297. len += FC_FDMI_HBA_ATTR_MODEL_LEN;
  1298. len += FC_FDMI_HBA_ATTR_MODELDESCR_LEN;
  1299. len += FC_FDMI_HBA_ATTR_HARDWAREVERSION_LEN;
  1300. len += FC_FDMI_HBA_ATTR_DRIVERVERSION_LEN;
  1301. len += FC_FDMI_HBA_ATTR_OPTIONROMVERSION_LEN;
  1302. len += FC_FDMI_HBA_ATTR_FIRMWAREVERSION_LEN;
  1303. len += FC_FDMI_HBA_ATTR_OSNAMEVERSION_LEN;
  1304. len += FC_FDMI_HBA_ATTR_MAXCTPAYLOAD_LEN;
  1305. if (fc_host->fdmi_version == FDMI_V2) {
  1306. numattrs += 7;
  1307. len += FC_FDMI_HBA_ATTR_NODESYMBLNAME_LEN;
  1308. len += FC_FDMI_HBA_ATTR_VENDORSPECIFICINFO_LEN;
  1309. len += FC_FDMI_HBA_ATTR_NUMBEROFPORTS_LEN;
  1310. len += FC_FDMI_HBA_ATTR_FABRICNAME_LEN;
  1311. len += FC_FDMI_HBA_ATTR_BIOSVERSION_LEN;
  1312. len += FC_FDMI_HBA_ATTR_BIOSSTATE_LEN;
  1313. len += FC_FDMI_HBA_ATTR_VENDORIDENTIFIER_LEN;
  1314. }
  1315. len += (numattrs * FC_FDMI_ATTR_ENTRY_HEADER_LEN);
  1316. size += len;
  1317. break;
  1318. case LPORT_ST_RPA:
  1319. cmd = FC_FDMI_RPA;
  1320. /* Number of Port Attributes */
  1321. numattrs = 6;
  1322. len = sizeof(struct fc_fdmi_rpa);
  1323. len -= sizeof(struct fc_fdmi_attr_entry);
  1324. len += FC_FDMI_PORT_ATTR_FC4TYPES_LEN;
  1325. len += FC_FDMI_PORT_ATTR_SUPPORTEDSPEED_LEN;
  1326. len += FC_FDMI_PORT_ATTR_CURRENTPORTSPEED_LEN;
  1327. len += FC_FDMI_PORT_ATTR_MAXFRAMESIZE_LEN;
  1328. len += FC_FDMI_PORT_ATTR_OSDEVICENAME_LEN;
  1329. len += FC_FDMI_PORT_ATTR_HOSTNAME_LEN;
  1330. if (fc_host->fdmi_version == FDMI_V2) {
  1331. numattrs += 10;
  1332. len += FC_FDMI_PORT_ATTR_NODENAME_LEN;
  1333. len += FC_FDMI_PORT_ATTR_PORTNAME_LEN;
  1334. len += FC_FDMI_PORT_ATTR_SYMBOLICNAME_LEN;
  1335. len += FC_FDMI_PORT_ATTR_PORTTYPE_LEN;
  1336. len += FC_FDMI_PORT_ATTR_SUPPORTEDCLASSSRVC_LEN;
  1337. len += FC_FDMI_PORT_ATTR_FABRICNAME_LEN;
  1338. len += FC_FDMI_PORT_ATTR_CURRENTFC4TYPE_LEN;
  1339. len += FC_FDMI_PORT_ATTR_PORTSTATE_LEN;
  1340. len += FC_FDMI_PORT_ATTR_DISCOVEREDPORTS_LEN;
  1341. len += FC_FDMI_PORT_ATTR_PORTID_LEN;
  1342. }
  1343. len += (numattrs * FC_FDMI_ATTR_ENTRY_HEADER_LEN);
  1344. size += len;
  1345. break;
  1346. case LPORT_ST_DPRT:
  1347. cmd = FC_FDMI_DPRT;
  1348. len = sizeof(struct fc_fdmi_dprt);
  1349. size += len;
  1350. break;
  1351. case LPORT_ST_DHBA:
  1352. cmd = FC_FDMI_DHBA;
  1353. len = sizeof(struct fc_fdmi_dhba);
  1354. size += len;
  1355. break;
  1356. default:
  1357. fc_lport_error(lport, NULL);
  1358. return;
  1359. }
  1360. FC_LPORT_DBG(lport, "Cmd=0x%x Len %d size %d\n",
  1361. cmd, (int)len, size);
  1362. fp = fc_frame_alloc(lport, size);
  1363. if (!fp) {
  1364. fc_lport_error(lport, fp);
  1365. return;
  1366. }
  1367. if (!lport->tt.elsct_send(lport, FC_FID_MGMT_SERV, fp, cmd,
  1368. fc_lport_ms_resp,
  1369. lport, 3 * lport->r_a_tov))
  1370. fc_lport_error(lport, fp);
  1371. }
  1372. /**
  1373. * fc_lport_enter_fdmi() - Create a fc_rport for the management server
  1374. * @lport: The local port requesting a remote port for the management server
  1375. */
  1376. static void fc_lport_enter_fdmi(struct fc_lport *lport)
  1377. {
  1378. struct fc_rport_priv *rdata;
  1379. lockdep_assert_held(&lport->lp_mutex);
  1380. FC_LPORT_DBG(lport, "Entered FDMI state from %s state\n",
  1381. fc_lport_state(lport));
  1382. fc_lport_state_enter(lport, LPORT_ST_FDMI);
  1383. mutex_lock(&lport->disc.disc_mutex);
  1384. rdata = fc_rport_create(lport, FC_FID_MGMT_SERV);
  1385. mutex_unlock(&lport->disc.disc_mutex);
  1386. if (!rdata)
  1387. goto err;
  1388. rdata->ops = &fc_lport_rport_ops;
  1389. fc_rport_login(rdata);
  1390. return;
  1391. err:
  1392. fc_lport_error(lport, NULL);
  1393. }
  1394. /**
  1395. * fc_lport_timeout() - Handler for the retry_work timer
  1396. * @work: The work struct of the local port
  1397. */
  1398. static void fc_lport_timeout(struct work_struct *work)
  1399. {
  1400. struct fc_lport *lport =
  1401. container_of(work, struct fc_lport,
  1402. retry_work.work);
  1403. struct fc_host_attrs *fc_host = shost_to_fc_host(lport->host);
  1404. mutex_lock(&lport->lp_mutex);
  1405. switch (lport->state) {
  1406. case LPORT_ST_DISABLED:
  1407. break;
  1408. case LPORT_ST_READY:
  1409. break;
  1410. case LPORT_ST_RESET:
  1411. break;
  1412. case LPORT_ST_FLOGI:
  1413. fc_lport_enter_flogi(lport);
  1414. break;
  1415. case LPORT_ST_DNS:
  1416. fc_lport_enter_dns(lport);
  1417. break;
  1418. case LPORT_ST_RNN_ID:
  1419. case LPORT_ST_RSNN_NN:
  1420. case LPORT_ST_RSPN_ID:
  1421. case LPORT_ST_RFT_ID:
  1422. case LPORT_ST_RFF_ID:
  1423. fc_lport_enter_ns(lport, lport->state);
  1424. break;
  1425. case LPORT_ST_FDMI:
  1426. fc_lport_enter_fdmi(lport);
  1427. break;
  1428. case LPORT_ST_RHBA:
  1429. if (fc_host->fdmi_version == FDMI_V2) {
  1430. FC_LPORT_DBG(lport, "timeout for FDMI-V2 RHBA,fall back to FDMI-V1\n");
  1431. fc_host->fdmi_version = FDMI_V1;
  1432. fc_lport_enter_ms(lport, LPORT_ST_RHBA);
  1433. break;
  1434. }
  1435. fallthrough;
  1436. case LPORT_ST_RPA:
  1437. case LPORT_ST_DHBA:
  1438. case LPORT_ST_DPRT:
  1439. FC_LPORT_DBG(lport, "Skipping lport state %s to SCR\n",
  1440. fc_lport_state(lport));
  1441. fallthrough;
  1442. case LPORT_ST_SCR:
  1443. fc_lport_enter_scr(lport);
  1444. break;
  1445. case LPORT_ST_LOGO:
  1446. fc_lport_enter_logo(lport);
  1447. break;
  1448. }
  1449. mutex_unlock(&lport->lp_mutex);
  1450. }
  1451. /**
  1452. * fc_lport_logo_resp() - Handle response to LOGO request
  1453. * @sp: The sequence that the LOGO was on
  1454. * @fp: The LOGO frame
  1455. * @lp_arg: The lport port that received the LOGO request
  1456. *
  1457. * Locking Note: This function will be called without the lport lock
  1458. * held, but it will lock, call an _enter_* function or fc_lport_error()
  1459. * and then unlock the lport.
  1460. */
  1461. void fc_lport_logo_resp(struct fc_seq *sp, struct fc_frame *fp,
  1462. void *lp_arg)
  1463. {
  1464. struct fc_lport *lport = lp_arg;
  1465. u8 op;
  1466. FC_LPORT_DBG(lport, "Received a LOGO %s\n", fc_els_resp_type(fp));
  1467. if (fp == ERR_PTR(-FC_EX_CLOSED))
  1468. return;
  1469. mutex_lock(&lport->lp_mutex);
  1470. if (lport->state != LPORT_ST_LOGO) {
  1471. FC_LPORT_DBG(lport, "Received a LOGO response, but in state "
  1472. "%s\n", fc_lport_state(lport));
  1473. if (IS_ERR(fp))
  1474. goto err;
  1475. goto out;
  1476. }
  1477. if (IS_ERR(fp)) {
  1478. fc_lport_error(lport, fp);
  1479. goto err;
  1480. }
  1481. op = fc_frame_payload_op(fp);
  1482. if (op == ELS_LS_ACC)
  1483. fc_lport_enter_disabled(lport);
  1484. else
  1485. fc_lport_error(lport, fp);
  1486. out:
  1487. fc_frame_free(fp);
  1488. err:
  1489. mutex_unlock(&lport->lp_mutex);
  1490. }
  1491. EXPORT_SYMBOL(fc_lport_logo_resp);
  1492. /**
  1493. * fc_lport_enter_logo() - Logout of the fabric
  1494. * @lport: The local port to be logged out
  1495. */
  1496. static void fc_lport_enter_logo(struct fc_lport *lport)
  1497. {
  1498. struct fc_frame *fp;
  1499. struct fc_els_logo *logo;
  1500. lockdep_assert_held(&lport->lp_mutex);
  1501. FC_LPORT_DBG(lport, "Entered LOGO state from %s state\n",
  1502. fc_lport_state(lport));
  1503. fc_lport_state_enter(lport, LPORT_ST_LOGO);
  1504. fc_vports_linkchange(lport);
  1505. fp = fc_frame_alloc(lport, sizeof(*logo));
  1506. if (!fp) {
  1507. fc_lport_error(lport, fp);
  1508. return;
  1509. }
  1510. if (!lport->tt.elsct_send(lport, FC_FID_FLOGI, fp, ELS_LOGO,
  1511. fc_lport_logo_resp, lport,
  1512. 2 * lport->r_a_tov))
  1513. fc_lport_error(lport, NULL);
  1514. }
  1515. /**
  1516. * fc_lport_flogi_resp() - Handle response to FLOGI request
  1517. * @sp: The sequence that the FLOGI was on
  1518. * @fp: The FLOGI response frame
  1519. * @lp_arg: The lport port that received the FLOGI response
  1520. *
  1521. * Locking Note: This function will be called without the lport lock
  1522. * held, but it will lock, call an _enter_* function or fc_lport_error()
  1523. * and then unlock the lport.
  1524. */
  1525. void fc_lport_flogi_resp(struct fc_seq *sp, struct fc_frame *fp,
  1526. void *lp_arg)
  1527. {
  1528. struct fc_lport *lport = lp_arg;
  1529. struct fc_frame_header *fh;
  1530. struct fc_els_flogi *flp;
  1531. u32 did;
  1532. u16 csp_flags;
  1533. unsigned int r_a_tov;
  1534. unsigned int e_d_tov;
  1535. u16 mfs;
  1536. FC_LPORT_DBG(lport, "Received a FLOGI %s\n", fc_els_resp_type(fp));
  1537. if (fp == ERR_PTR(-FC_EX_CLOSED))
  1538. return;
  1539. mutex_lock(&lport->lp_mutex);
  1540. if (lport->state != LPORT_ST_FLOGI) {
  1541. FC_LPORT_DBG(lport, "Received a FLOGI response, but in state "
  1542. "%s\n", fc_lport_state(lport));
  1543. if (IS_ERR(fp))
  1544. goto err;
  1545. goto out;
  1546. }
  1547. if (IS_ERR(fp)) {
  1548. fc_lport_error(lport, fp);
  1549. goto err;
  1550. }
  1551. fh = fc_frame_header_get(fp);
  1552. did = fc_frame_did(fp);
  1553. if (fh->fh_r_ctl != FC_RCTL_ELS_REP || did == 0 ||
  1554. fc_frame_payload_op(fp) != ELS_LS_ACC) {
  1555. FC_LPORT_DBG(lport, "FLOGI not accepted or bad response\n");
  1556. fc_lport_error(lport, fp);
  1557. goto out;
  1558. }
  1559. flp = fc_frame_payload_get(fp, sizeof(*flp));
  1560. if (!flp) {
  1561. FC_LPORT_DBG(lport, "FLOGI bad response\n");
  1562. fc_lport_error(lport, fp);
  1563. goto out;
  1564. }
  1565. mfs = ntohs(flp->fl_csp.sp_bb_data) &
  1566. FC_SP_BB_DATA_MASK;
  1567. if (mfs < FC_SP_MIN_MAX_PAYLOAD || mfs > FC_SP_MAX_MAX_PAYLOAD) {
  1568. FC_LPORT_DBG(lport, "FLOGI bad mfs:%hu response, "
  1569. "lport->mfs:%u\n", mfs, lport->mfs);
  1570. fc_lport_error(lport, fp);
  1571. goto out;
  1572. }
  1573. if (mfs <= lport->mfs) {
  1574. lport->mfs = mfs;
  1575. fc_host_maxframe_size(lport->host) = mfs;
  1576. }
  1577. csp_flags = ntohs(flp->fl_csp.sp_features);
  1578. r_a_tov = ntohl(flp->fl_csp.sp_r_a_tov);
  1579. e_d_tov = ntohl(flp->fl_csp.sp_e_d_tov);
  1580. if (csp_flags & FC_SP_FT_EDTR)
  1581. e_d_tov /= 1000000;
  1582. lport->npiv_enabled = !!(csp_flags & FC_SP_FT_NPIV_ACC);
  1583. if ((csp_flags & FC_SP_FT_FPORT) == 0) {
  1584. if (e_d_tov > lport->e_d_tov)
  1585. lport->e_d_tov = e_d_tov;
  1586. lport->r_a_tov = 2 * lport->e_d_tov;
  1587. fc_lport_set_port_id(lport, did, fp);
  1588. printk(KERN_INFO "host%d: libfc: "
  1589. "Port (%6.6x) entered "
  1590. "point-to-point mode\n",
  1591. lport->host->host_no, did);
  1592. fc_lport_ptp_setup(lport, fc_frame_sid(fp),
  1593. get_unaligned_be64(
  1594. &flp->fl_wwpn),
  1595. get_unaligned_be64(
  1596. &flp->fl_wwnn));
  1597. } else {
  1598. if (e_d_tov > lport->e_d_tov)
  1599. lport->e_d_tov = e_d_tov;
  1600. if (r_a_tov > lport->r_a_tov)
  1601. lport->r_a_tov = r_a_tov;
  1602. fc_host_fabric_name(lport->host) =
  1603. get_unaligned_be64(&flp->fl_wwnn);
  1604. fc_lport_set_port_id(lport, did, fp);
  1605. fc_lport_enter_dns(lport);
  1606. }
  1607. out:
  1608. fc_frame_free(fp);
  1609. err:
  1610. mutex_unlock(&lport->lp_mutex);
  1611. }
  1612. EXPORT_SYMBOL(fc_lport_flogi_resp);
  1613. /**
  1614. * fc_lport_enter_flogi() - Send a FLOGI request to the fabric manager
  1615. * @lport: Fibre Channel local port to be logged in to the fabric
  1616. */
  1617. static void fc_lport_enter_flogi(struct fc_lport *lport)
  1618. {
  1619. struct fc_frame *fp;
  1620. lockdep_assert_held(&lport->lp_mutex);
  1621. FC_LPORT_DBG(lport, "Entered FLOGI state from %s state\n",
  1622. fc_lport_state(lport));
  1623. fc_lport_state_enter(lport, LPORT_ST_FLOGI);
  1624. if (lport->point_to_multipoint) {
  1625. if (lport->port_id)
  1626. fc_lport_enter_ready(lport);
  1627. return;
  1628. }
  1629. fp = fc_frame_alloc(lport, sizeof(struct fc_els_flogi));
  1630. if (!fp)
  1631. return fc_lport_error(lport, fp);
  1632. if (!lport->tt.elsct_send(lport, FC_FID_FLOGI, fp,
  1633. lport->vport ? ELS_FDISC : ELS_FLOGI,
  1634. fc_lport_flogi_resp, lport,
  1635. lport->vport ? 2 * lport->r_a_tov :
  1636. lport->e_d_tov))
  1637. fc_lport_error(lport, NULL);
  1638. }
  1639. /**
  1640. * fc_lport_config() - Configure a fc_lport
  1641. * @lport: The local port to be configured
  1642. */
  1643. int fc_lport_config(struct fc_lport *lport)
  1644. {
  1645. INIT_DELAYED_WORK(&lport->retry_work, fc_lport_timeout);
  1646. mutex_init(&lport->lp_mutex);
  1647. fc_lport_state_enter(lport, LPORT_ST_DISABLED);
  1648. fc_lport_add_fc4_type(lport, FC_TYPE_FCP);
  1649. fc_lport_add_fc4_type(lport, FC_TYPE_CT);
  1650. fc_fc4_conf_lport_params(lport, FC_TYPE_FCP);
  1651. return 0;
  1652. }
  1653. EXPORT_SYMBOL(fc_lport_config);
  1654. /**
  1655. * fc_lport_init() - Initialize the lport layer for a local port
  1656. * @lport: The local port to initialize the exchange layer for
  1657. */
  1658. int fc_lport_init(struct fc_lport *lport)
  1659. {
  1660. struct fc_host_attrs *fc_host;
  1661. fc_host = shost_to_fc_host(lport->host);
  1662. /* Set FDMI version to FDMI-2 specification*/
  1663. fc_host->fdmi_version = FDMI_V2;
  1664. fc_host_port_type(lport->host) = FC_PORTTYPE_NPORT;
  1665. fc_host_node_name(lport->host) = lport->wwnn;
  1666. fc_host_port_name(lport->host) = lport->wwpn;
  1667. fc_host_supported_classes(lport->host) = FC_COS_CLASS3;
  1668. memset(fc_host_supported_fc4s(lport->host), 0,
  1669. sizeof(fc_host_supported_fc4s(lport->host)));
  1670. fc_host_supported_fc4s(lport->host)[2] = 1;
  1671. fc_host_supported_fc4s(lport->host)[7] = 1;
  1672. fc_host_num_discovered_ports(lport->host) = 4;
  1673. /* This value is also unchanging */
  1674. memset(fc_host_active_fc4s(lport->host), 0,
  1675. sizeof(fc_host_active_fc4s(lport->host)));
  1676. fc_host_active_fc4s(lport->host)[2] = 1;
  1677. fc_host_active_fc4s(lport->host)[7] = 1;
  1678. fc_host_maxframe_size(lport->host) = lport->mfs;
  1679. fc_host_supported_speeds(lport->host) = 0;
  1680. if (lport->link_supported_speeds & FC_PORTSPEED_1GBIT)
  1681. fc_host_supported_speeds(lport->host) |= FC_PORTSPEED_1GBIT;
  1682. if (lport->link_supported_speeds & FC_PORTSPEED_10GBIT)
  1683. fc_host_supported_speeds(lport->host) |= FC_PORTSPEED_10GBIT;
  1684. if (lport->link_supported_speeds & FC_PORTSPEED_40GBIT)
  1685. fc_host_supported_speeds(lport->host) |= FC_PORTSPEED_40GBIT;
  1686. if (lport->link_supported_speeds & FC_PORTSPEED_100GBIT)
  1687. fc_host_supported_speeds(lport->host) |= FC_PORTSPEED_100GBIT;
  1688. if (lport->link_supported_speeds & FC_PORTSPEED_25GBIT)
  1689. fc_host_supported_speeds(lport->host) |= FC_PORTSPEED_25GBIT;
  1690. if (lport->link_supported_speeds & FC_PORTSPEED_50GBIT)
  1691. fc_host_supported_speeds(lport->host) |= FC_PORTSPEED_50GBIT;
  1692. if (lport->link_supported_speeds & FC_PORTSPEED_100GBIT)
  1693. fc_host_supported_speeds(lport->host) |= FC_PORTSPEED_100GBIT;
  1694. fc_fc4_add_lport(lport);
  1695. fc_host_num_discovered_ports(lport->host) = DISCOVERED_PORTS;
  1696. fc_host_port_state(lport->host) = FC_PORTSTATE_ONLINE;
  1697. fc_host_max_ct_payload(lport->host) = MAX_CT_PAYLOAD;
  1698. fc_host_num_ports(lport->host) = NUMBER_OF_PORTS;
  1699. fc_host_bootbios_state(lport->host) = 0X00000000;
  1700. snprintf(fc_host_bootbios_version(lport->host),
  1701. FC_SYMBOLIC_NAME_SIZE, "%s", "Unknown");
  1702. return 0;
  1703. }
  1704. EXPORT_SYMBOL(fc_lport_init);
  1705. /**
  1706. * fc_lport_bsg_resp() - The common response handler for FC Passthrough requests
  1707. * @sp: The sequence for the FC Passthrough response
  1708. * @fp: The response frame
  1709. * @info_arg: The BSG info that the response is for
  1710. */
  1711. static void fc_lport_bsg_resp(struct fc_seq *sp, struct fc_frame *fp,
  1712. void *info_arg)
  1713. {
  1714. struct fc_bsg_info *info = info_arg;
  1715. struct bsg_job *job = info->job;
  1716. struct fc_bsg_reply *bsg_reply = job->reply;
  1717. struct fc_lport *lport = info->lport;
  1718. struct fc_frame_header *fh;
  1719. size_t len;
  1720. void *buf;
  1721. if (IS_ERR(fp)) {
  1722. bsg_reply->result = (PTR_ERR(fp) == -FC_EX_CLOSED) ?
  1723. -ECONNABORTED : -ETIMEDOUT;
  1724. job->reply_len = sizeof(uint32_t);
  1725. bsg_job_done(job, bsg_reply->result,
  1726. bsg_reply->reply_payload_rcv_len);
  1727. kfree(info);
  1728. return;
  1729. }
  1730. mutex_lock(&lport->lp_mutex);
  1731. fh = fc_frame_header_get(fp);
  1732. len = fr_len(fp) - sizeof(*fh);
  1733. buf = fc_frame_payload_get(fp, 0);
  1734. if (fr_sof(fp) == FC_SOF_I3 && !ntohs(fh->fh_seq_cnt)) {
  1735. /* Get the response code from the first frame payload */
  1736. unsigned short cmd = (info->rsp_code == FC_FS_ACC) ?
  1737. ntohs(((struct fc_ct_hdr *)buf)->ct_cmd) :
  1738. (unsigned short)fc_frame_payload_op(fp);
  1739. /* Save the reply status of the job */
  1740. bsg_reply->reply_data.ctels_reply.status =
  1741. (cmd == info->rsp_code) ?
  1742. FC_CTELS_STATUS_OK : FC_CTELS_STATUS_REJECT;
  1743. }
  1744. bsg_reply->reply_payload_rcv_len +=
  1745. fc_copy_buffer_to_sglist(buf, len, info->sg, &info->nents,
  1746. &info->offset, NULL);
  1747. if (fr_eof(fp) == FC_EOF_T &&
  1748. (ntoh24(fh->fh_f_ctl) & (FC_FC_LAST_SEQ | FC_FC_END_SEQ)) ==
  1749. (FC_FC_LAST_SEQ | FC_FC_END_SEQ)) {
  1750. if (bsg_reply->reply_payload_rcv_len >
  1751. job->reply_payload.payload_len)
  1752. bsg_reply->reply_payload_rcv_len =
  1753. job->reply_payload.payload_len;
  1754. bsg_reply->result = 0;
  1755. bsg_job_done(job, bsg_reply->result,
  1756. bsg_reply->reply_payload_rcv_len);
  1757. kfree(info);
  1758. }
  1759. fc_frame_free(fp);
  1760. mutex_unlock(&lport->lp_mutex);
  1761. }
  1762. /**
  1763. * fc_lport_els_request() - Send ELS passthrough request
  1764. * @job: The BSG Passthrough job
  1765. * @lport: The local port sending the request
  1766. * @did: The destination port id
  1767. * @tov: The timeout period (in ms)
  1768. */
  1769. static int fc_lport_els_request(struct bsg_job *job,
  1770. struct fc_lport *lport,
  1771. u32 did, u32 tov)
  1772. {
  1773. struct fc_bsg_info *info;
  1774. struct fc_frame *fp;
  1775. struct fc_frame_header *fh;
  1776. char *pp;
  1777. int len;
  1778. lockdep_assert_held(&lport->lp_mutex);
  1779. fp = fc_frame_alloc(lport, job->request_payload.payload_len);
  1780. if (!fp)
  1781. return -ENOMEM;
  1782. len = job->request_payload.payload_len;
  1783. pp = fc_frame_payload_get(fp, len);
  1784. sg_copy_to_buffer(job->request_payload.sg_list,
  1785. job->request_payload.sg_cnt,
  1786. pp, len);
  1787. fh = fc_frame_header_get(fp);
  1788. fh->fh_r_ctl = FC_RCTL_ELS_REQ;
  1789. hton24(fh->fh_d_id, did);
  1790. hton24(fh->fh_s_id, lport->port_id);
  1791. fh->fh_type = FC_TYPE_ELS;
  1792. hton24(fh->fh_f_ctl, FC_FCTL_REQ);
  1793. fh->fh_cs_ctl = 0;
  1794. fh->fh_df_ctl = 0;
  1795. fh->fh_parm_offset = 0;
  1796. info = kzalloc(sizeof(struct fc_bsg_info), GFP_KERNEL);
  1797. if (!info) {
  1798. fc_frame_free(fp);
  1799. return -ENOMEM;
  1800. }
  1801. info->job = job;
  1802. info->lport = lport;
  1803. info->rsp_code = ELS_LS_ACC;
  1804. info->nents = job->reply_payload.sg_cnt;
  1805. info->sg = job->reply_payload.sg_list;
  1806. if (!fc_exch_seq_send(lport, fp, fc_lport_bsg_resp,
  1807. NULL, info, tov)) {
  1808. kfree(info);
  1809. return -ECOMM;
  1810. }
  1811. return 0;
  1812. }
  1813. /**
  1814. * fc_lport_ct_request() - Send CT Passthrough request
  1815. * @job: The BSG Passthrough job
  1816. * @lport: The local port sending the request
  1817. * @did: The destination FC-ID
  1818. * @tov: The timeout period to wait for the response
  1819. */
  1820. static int fc_lport_ct_request(struct bsg_job *job,
  1821. struct fc_lport *lport, u32 did, u32 tov)
  1822. {
  1823. struct fc_bsg_info *info;
  1824. struct fc_frame *fp;
  1825. struct fc_frame_header *fh;
  1826. struct fc_ct_req *ct;
  1827. size_t len;
  1828. lockdep_assert_held(&lport->lp_mutex);
  1829. fp = fc_frame_alloc(lport, sizeof(struct fc_ct_hdr) +
  1830. job->request_payload.payload_len);
  1831. if (!fp)
  1832. return -ENOMEM;
  1833. len = job->request_payload.payload_len;
  1834. ct = fc_frame_payload_get(fp, len);
  1835. sg_copy_to_buffer(job->request_payload.sg_list,
  1836. job->request_payload.sg_cnt,
  1837. ct, len);
  1838. fh = fc_frame_header_get(fp);
  1839. fh->fh_r_ctl = FC_RCTL_DD_UNSOL_CTL;
  1840. hton24(fh->fh_d_id, did);
  1841. hton24(fh->fh_s_id, lport->port_id);
  1842. fh->fh_type = FC_TYPE_CT;
  1843. hton24(fh->fh_f_ctl, FC_FCTL_REQ);
  1844. fh->fh_cs_ctl = 0;
  1845. fh->fh_df_ctl = 0;
  1846. fh->fh_parm_offset = 0;
  1847. info = kzalloc(sizeof(struct fc_bsg_info), GFP_KERNEL);
  1848. if (!info) {
  1849. fc_frame_free(fp);
  1850. return -ENOMEM;
  1851. }
  1852. info->job = job;
  1853. info->lport = lport;
  1854. info->rsp_code = FC_FS_ACC;
  1855. info->nents = job->reply_payload.sg_cnt;
  1856. info->sg = job->reply_payload.sg_list;
  1857. if (!fc_exch_seq_send(lport, fp, fc_lport_bsg_resp,
  1858. NULL, info, tov)) {
  1859. kfree(info);
  1860. return -ECOMM;
  1861. }
  1862. return 0;
  1863. }
  1864. /**
  1865. * fc_lport_bsg_request() - The common entry point for sending
  1866. * FC Passthrough requests
  1867. * @job: The BSG passthrough job
  1868. */
  1869. int fc_lport_bsg_request(struct bsg_job *job)
  1870. {
  1871. struct fc_bsg_request *bsg_request = job->request;
  1872. struct fc_bsg_reply *bsg_reply = job->reply;
  1873. struct Scsi_Host *shost = fc_bsg_to_shost(job);
  1874. struct fc_lport *lport = shost_priv(shost);
  1875. struct fc_rport *rport;
  1876. struct fc_rport_priv *rdata;
  1877. int rc = -EINVAL;
  1878. u32 did, tov;
  1879. bsg_reply->reply_payload_rcv_len = 0;
  1880. mutex_lock(&lport->lp_mutex);
  1881. switch (bsg_request->msgcode) {
  1882. case FC_BSG_RPT_ELS:
  1883. rport = fc_bsg_to_rport(job);
  1884. if (!rport)
  1885. break;
  1886. rdata = rport->dd_data;
  1887. rc = fc_lport_els_request(job, lport, rport->port_id,
  1888. rdata->e_d_tov);
  1889. break;
  1890. case FC_BSG_RPT_CT:
  1891. rport = fc_bsg_to_rport(job);
  1892. if (!rport)
  1893. break;
  1894. rdata = rport->dd_data;
  1895. rc = fc_lport_ct_request(job, lport, rport->port_id,
  1896. rdata->e_d_tov);
  1897. break;
  1898. case FC_BSG_HST_CT:
  1899. did = ntoh24(bsg_request->rqst_data.h_ct.port_id);
  1900. if (did == FC_FID_DIR_SERV) {
  1901. rdata = lport->dns_rdata;
  1902. if (!rdata)
  1903. break;
  1904. tov = rdata->e_d_tov;
  1905. } else {
  1906. rdata = fc_rport_lookup(lport, did);
  1907. if (!rdata)
  1908. break;
  1909. tov = rdata->e_d_tov;
  1910. kref_put(&rdata->kref, fc_rport_destroy);
  1911. }
  1912. rc = fc_lport_ct_request(job, lport, did, tov);
  1913. break;
  1914. case FC_BSG_HST_ELS_NOLOGIN:
  1915. did = ntoh24(bsg_request->rqst_data.h_els.port_id);
  1916. rc = fc_lport_els_request(job, lport, did, lport->e_d_tov);
  1917. break;
  1918. }
  1919. mutex_unlock(&lport->lp_mutex);
  1920. return rc;
  1921. }
  1922. EXPORT_SYMBOL(fc_lport_bsg_request);