fc_lport.c 56 KB

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