fc_lport.c 56 KB

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