fc_lport.c 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144
  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 expected 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. IS_ERR(fp) ? -PTR_ERR(fp) : 0, 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_DBG(lport, "Skipping lport state %s to SCR\n",
  1431. fc_lport_state(lport));
  1432. /* fall thru */
  1433. case LPORT_ST_SCR:
  1434. fc_lport_enter_scr(lport);
  1435. break;
  1436. case LPORT_ST_LOGO:
  1437. fc_lport_enter_logo(lport);
  1438. break;
  1439. }
  1440. mutex_unlock(&lport->lp_mutex);
  1441. }
  1442. /**
  1443. * fc_lport_logo_resp() - Handle response to LOGO request
  1444. * @sp: The sequence that the LOGO was on
  1445. * @fp: The LOGO frame
  1446. * @lp_arg: The lport port that received the LOGO request
  1447. *
  1448. * Locking Note: This function will be called without the lport lock
  1449. * held, but it will lock, call an _enter_* function or fc_lport_error()
  1450. * and then unlock the lport.
  1451. */
  1452. void fc_lport_logo_resp(struct fc_seq *sp, struct fc_frame *fp,
  1453. void *lp_arg)
  1454. {
  1455. struct fc_lport *lport = lp_arg;
  1456. u8 op;
  1457. FC_LPORT_DBG(lport, "Received a LOGO %s\n", fc_els_resp_type(fp));
  1458. if (fp == ERR_PTR(-FC_EX_CLOSED))
  1459. return;
  1460. mutex_lock(&lport->lp_mutex);
  1461. if (lport->state != LPORT_ST_LOGO) {
  1462. FC_LPORT_DBG(lport, "Received a LOGO response, but in state "
  1463. "%s\n", fc_lport_state(lport));
  1464. if (IS_ERR(fp))
  1465. goto err;
  1466. goto out;
  1467. }
  1468. if (IS_ERR(fp)) {
  1469. fc_lport_error(lport, fp);
  1470. goto err;
  1471. }
  1472. op = fc_frame_payload_op(fp);
  1473. if (op == ELS_LS_ACC)
  1474. fc_lport_enter_disabled(lport);
  1475. else
  1476. fc_lport_error(lport, fp);
  1477. out:
  1478. fc_frame_free(fp);
  1479. err:
  1480. mutex_unlock(&lport->lp_mutex);
  1481. }
  1482. EXPORT_SYMBOL(fc_lport_logo_resp);
  1483. /**
  1484. * fc_rport_enter_logo() - Logout of the fabric
  1485. * @lport: The local port to be logged out
  1486. *
  1487. * Locking Note: The lport lock is expected to be held before calling
  1488. * this routine.
  1489. */
  1490. static void fc_lport_enter_logo(struct fc_lport *lport)
  1491. {
  1492. struct fc_frame *fp;
  1493. struct fc_els_logo *logo;
  1494. FC_LPORT_DBG(lport, "Entered LOGO state from %s state\n",
  1495. fc_lport_state(lport));
  1496. fc_lport_state_enter(lport, LPORT_ST_LOGO);
  1497. fc_vports_linkchange(lport);
  1498. fp = fc_frame_alloc(lport, sizeof(*logo));
  1499. if (!fp) {
  1500. fc_lport_error(lport, fp);
  1501. return;
  1502. }
  1503. if (!lport->tt.elsct_send(lport, FC_FID_FLOGI, fp, ELS_LOGO,
  1504. fc_lport_logo_resp, lport,
  1505. 2 * lport->r_a_tov))
  1506. fc_lport_error(lport, NULL);
  1507. }
  1508. /**
  1509. * fc_lport_flogi_resp() - Handle response to FLOGI request
  1510. * @sp: The sequence that the FLOGI was on
  1511. * @fp: The FLOGI response frame
  1512. * @lp_arg: The lport port that received the FLOGI response
  1513. *
  1514. * Locking Note: This function will be called without the lport lock
  1515. * held, but it will lock, call an _enter_* function or fc_lport_error()
  1516. * and then unlock the lport.
  1517. */
  1518. void fc_lport_flogi_resp(struct fc_seq *sp, struct fc_frame *fp,
  1519. void *lp_arg)
  1520. {
  1521. struct fc_lport *lport = lp_arg;
  1522. struct fc_frame_header *fh;
  1523. struct fc_els_flogi *flp;
  1524. u32 did;
  1525. u16 csp_flags;
  1526. unsigned int r_a_tov;
  1527. unsigned int e_d_tov;
  1528. u16 mfs;
  1529. FC_LPORT_DBG(lport, "Received a FLOGI %s\n", fc_els_resp_type(fp));
  1530. if (fp == ERR_PTR(-FC_EX_CLOSED))
  1531. return;
  1532. mutex_lock(&lport->lp_mutex);
  1533. if (lport->state != LPORT_ST_FLOGI) {
  1534. FC_LPORT_DBG(lport, "Received a FLOGI response, but in state "
  1535. "%s\n", fc_lport_state(lport));
  1536. if (IS_ERR(fp))
  1537. goto err;
  1538. goto out;
  1539. }
  1540. if (IS_ERR(fp)) {
  1541. fc_lport_error(lport, fp);
  1542. goto err;
  1543. }
  1544. fh = fc_frame_header_get(fp);
  1545. did = fc_frame_did(fp);
  1546. if (fh->fh_r_ctl != FC_RCTL_ELS_REP || did == 0 ||
  1547. fc_frame_payload_op(fp) != ELS_LS_ACC) {
  1548. FC_LPORT_DBG(lport, "FLOGI not accepted or bad response\n");
  1549. fc_lport_error(lport, fp);
  1550. goto out;
  1551. }
  1552. flp = fc_frame_payload_get(fp, sizeof(*flp));
  1553. if (!flp) {
  1554. FC_LPORT_DBG(lport, "FLOGI bad response\n");
  1555. fc_lport_error(lport, fp);
  1556. goto out;
  1557. }
  1558. mfs = ntohs(flp->fl_csp.sp_bb_data) &
  1559. FC_SP_BB_DATA_MASK;
  1560. if (mfs < FC_SP_MIN_MAX_PAYLOAD || mfs > FC_SP_MAX_MAX_PAYLOAD) {
  1561. FC_LPORT_DBG(lport, "FLOGI bad mfs:%hu response, "
  1562. "lport->mfs:%hu\n", mfs, lport->mfs);
  1563. fc_lport_error(lport, fp);
  1564. goto out;
  1565. }
  1566. if (mfs <= lport->mfs) {
  1567. lport->mfs = mfs;
  1568. fc_host_maxframe_size(lport->host) = mfs;
  1569. }
  1570. csp_flags = ntohs(flp->fl_csp.sp_features);
  1571. r_a_tov = ntohl(flp->fl_csp.sp_r_a_tov);
  1572. e_d_tov = ntohl(flp->fl_csp.sp_e_d_tov);
  1573. if (csp_flags & FC_SP_FT_EDTR)
  1574. e_d_tov /= 1000000;
  1575. lport->npiv_enabled = !!(csp_flags & FC_SP_FT_NPIV_ACC);
  1576. if ((csp_flags & FC_SP_FT_FPORT) == 0) {
  1577. if (e_d_tov > lport->e_d_tov)
  1578. lport->e_d_tov = e_d_tov;
  1579. lport->r_a_tov = 2 * e_d_tov;
  1580. fc_lport_set_port_id(lport, did, fp);
  1581. printk(KERN_INFO "host%d: libfc: "
  1582. "Port (%6.6x) entered "
  1583. "point-to-point mode\n",
  1584. lport->host->host_no, did);
  1585. fc_lport_ptp_setup(lport, fc_frame_sid(fp),
  1586. get_unaligned_be64(
  1587. &flp->fl_wwpn),
  1588. get_unaligned_be64(
  1589. &flp->fl_wwnn));
  1590. } else {
  1591. lport->e_d_tov = e_d_tov;
  1592. lport->r_a_tov = r_a_tov;
  1593. fc_host_fabric_name(lport->host) =
  1594. get_unaligned_be64(&flp->fl_wwnn);
  1595. fc_lport_set_port_id(lport, did, fp);
  1596. fc_lport_enter_dns(lport);
  1597. }
  1598. out:
  1599. fc_frame_free(fp);
  1600. err:
  1601. mutex_unlock(&lport->lp_mutex);
  1602. }
  1603. EXPORT_SYMBOL(fc_lport_flogi_resp);
  1604. /**
  1605. * fc_rport_enter_flogi() - Send a FLOGI request to the fabric manager
  1606. * @lport: Fibre Channel local port to be logged in to the fabric
  1607. *
  1608. * Locking Note: The lport lock is expected to be held before calling
  1609. * this routine.
  1610. */
  1611. static void fc_lport_enter_flogi(struct fc_lport *lport)
  1612. {
  1613. struct fc_frame *fp;
  1614. FC_LPORT_DBG(lport, "Entered FLOGI state from %s state\n",
  1615. fc_lport_state(lport));
  1616. fc_lport_state_enter(lport, LPORT_ST_FLOGI);
  1617. if (lport->point_to_multipoint) {
  1618. if (lport->port_id)
  1619. fc_lport_enter_ready(lport);
  1620. return;
  1621. }
  1622. fp = fc_frame_alloc(lport, sizeof(struct fc_els_flogi));
  1623. if (!fp)
  1624. return fc_lport_error(lport, fp);
  1625. if (!lport->tt.elsct_send(lport, FC_FID_FLOGI, fp,
  1626. lport->vport ? ELS_FDISC : ELS_FLOGI,
  1627. fc_lport_flogi_resp, lport,
  1628. lport->vport ? 2 * lport->r_a_tov :
  1629. lport->e_d_tov))
  1630. fc_lport_error(lport, NULL);
  1631. }
  1632. /**
  1633. * fc_lport_config() - Configure a fc_lport
  1634. * @lport: The local port to be configured
  1635. */
  1636. int fc_lport_config(struct fc_lport *lport)
  1637. {
  1638. INIT_DELAYED_WORK(&lport->retry_work, fc_lport_timeout);
  1639. mutex_init(&lport->lp_mutex);
  1640. fc_lport_state_enter(lport, LPORT_ST_DISABLED);
  1641. fc_lport_add_fc4_type(lport, FC_TYPE_FCP);
  1642. fc_lport_add_fc4_type(lport, FC_TYPE_CT);
  1643. fc_fc4_conf_lport_params(lport, FC_TYPE_FCP);
  1644. return 0;
  1645. }
  1646. EXPORT_SYMBOL(fc_lport_config);
  1647. /**
  1648. * fc_lport_init() - Initialize the lport layer for a local port
  1649. * @lport: The local port to initialize the exchange layer for
  1650. */
  1651. int fc_lport_init(struct fc_lport *lport)
  1652. {
  1653. if (!lport->tt.lport_recv)
  1654. lport->tt.lport_recv = fc_lport_recv_req;
  1655. if (!lport->tt.lport_reset)
  1656. lport->tt.lport_reset = fc_lport_reset;
  1657. fc_host_port_type(lport->host) = FC_PORTTYPE_NPORT;
  1658. fc_host_node_name(lport->host) = lport->wwnn;
  1659. fc_host_port_name(lport->host) = lport->wwpn;
  1660. fc_host_supported_classes(lport->host) = FC_COS_CLASS3;
  1661. memset(fc_host_supported_fc4s(lport->host), 0,
  1662. sizeof(fc_host_supported_fc4s(lport->host)));
  1663. fc_host_supported_fc4s(lport->host)[2] = 1;
  1664. fc_host_supported_fc4s(lport->host)[7] = 1;
  1665. /* This value is also unchanging */
  1666. memset(fc_host_active_fc4s(lport->host), 0,
  1667. sizeof(fc_host_active_fc4s(lport->host)));
  1668. fc_host_active_fc4s(lport->host)[2] = 1;
  1669. fc_host_active_fc4s(lport->host)[7] = 1;
  1670. fc_host_maxframe_size(lport->host) = lport->mfs;
  1671. fc_host_supported_speeds(lport->host) = 0;
  1672. if (lport->link_supported_speeds & FC_PORTSPEED_1GBIT)
  1673. fc_host_supported_speeds(lport->host) |= FC_PORTSPEED_1GBIT;
  1674. if (lport->link_supported_speeds & FC_PORTSPEED_10GBIT)
  1675. fc_host_supported_speeds(lport->host) |= FC_PORTSPEED_10GBIT;
  1676. fc_fc4_add_lport(lport);
  1677. return 0;
  1678. }
  1679. EXPORT_SYMBOL(fc_lport_init);
  1680. /**
  1681. * fc_lport_bsg_resp() - The common response handler for FC Passthrough requests
  1682. * @sp: The sequence for the FC Passthrough response
  1683. * @fp: The response frame
  1684. * @info_arg: The BSG info that the response is for
  1685. */
  1686. static void fc_lport_bsg_resp(struct fc_seq *sp, struct fc_frame *fp,
  1687. void *info_arg)
  1688. {
  1689. struct fc_bsg_info *info = info_arg;
  1690. struct fc_bsg_job *job = info->job;
  1691. struct fc_lport *lport = info->lport;
  1692. struct fc_frame_header *fh;
  1693. size_t len;
  1694. void *buf;
  1695. if (IS_ERR(fp)) {
  1696. job->reply->result = (PTR_ERR(fp) == -FC_EX_CLOSED) ?
  1697. -ECONNABORTED : -ETIMEDOUT;
  1698. job->reply_len = sizeof(uint32_t);
  1699. job->state_flags |= FC_RQST_STATE_DONE;
  1700. job->job_done(job);
  1701. kfree(info);
  1702. return;
  1703. }
  1704. mutex_lock(&lport->lp_mutex);
  1705. fh = fc_frame_header_get(fp);
  1706. len = fr_len(fp) - sizeof(*fh);
  1707. buf = fc_frame_payload_get(fp, 0);
  1708. if (fr_sof(fp) == FC_SOF_I3 && !ntohs(fh->fh_seq_cnt)) {
  1709. /* Get the response code from the first frame payload */
  1710. unsigned short cmd = (info->rsp_code == FC_FS_ACC) ?
  1711. ntohs(((struct fc_ct_hdr *)buf)->ct_cmd) :
  1712. (unsigned short)fc_frame_payload_op(fp);
  1713. /* Save the reply status of the job */
  1714. job->reply->reply_data.ctels_reply.status =
  1715. (cmd == info->rsp_code) ?
  1716. FC_CTELS_STATUS_OK : FC_CTELS_STATUS_REJECT;
  1717. }
  1718. job->reply->reply_payload_rcv_len +=
  1719. fc_copy_buffer_to_sglist(buf, len, info->sg, &info->nents,
  1720. &info->offset, NULL);
  1721. if (fr_eof(fp) == FC_EOF_T &&
  1722. (ntoh24(fh->fh_f_ctl) & (FC_FC_LAST_SEQ | FC_FC_END_SEQ)) ==
  1723. (FC_FC_LAST_SEQ | FC_FC_END_SEQ)) {
  1724. if (job->reply->reply_payload_rcv_len >
  1725. job->reply_payload.payload_len)
  1726. job->reply->reply_payload_rcv_len =
  1727. job->reply_payload.payload_len;
  1728. job->reply->result = 0;
  1729. job->state_flags |= FC_RQST_STATE_DONE;
  1730. job->job_done(job);
  1731. kfree(info);
  1732. }
  1733. fc_frame_free(fp);
  1734. mutex_unlock(&lport->lp_mutex);
  1735. }
  1736. /**
  1737. * fc_lport_els_request() - Send ELS passthrough request
  1738. * @job: The BSG Passthrough job
  1739. * @lport: The local port sending the request
  1740. * @did: The destination port id
  1741. *
  1742. * Locking Note: The lport lock is expected to be held before calling
  1743. * this routine.
  1744. */
  1745. static int fc_lport_els_request(struct fc_bsg_job *job,
  1746. struct fc_lport *lport,
  1747. u32 did, u32 tov)
  1748. {
  1749. struct fc_bsg_info *info;
  1750. struct fc_frame *fp;
  1751. struct fc_frame_header *fh;
  1752. char *pp;
  1753. int len;
  1754. fp = fc_frame_alloc(lport, job->request_payload.payload_len);
  1755. if (!fp)
  1756. return -ENOMEM;
  1757. len = job->request_payload.payload_len;
  1758. pp = fc_frame_payload_get(fp, len);
  1759. sg_copy_to_buffer(job->request_payload.sg_list,
  1760. job->request_payload.sg_cnt,
  1761. pp, len);
  1762. fh = fc_frame_header_get(fp);
  1763. fh->fh_r_ctl = FC_RCTL_ELS_REQ;
  1764. hton24(fh->fh_d_id, did);
  1765. hton24(fh->fh_s_id, lport->port_id);
  1766. fh->fh_type = FC_TYPE_ELS;
  1767. hton24(fh->fh_f_ctl, FC_FCTL_REQ);
  1768. fh->fh_cs_ctl = 0;
  1769. fh->fh_df_ctl = 0;
  1770. fh->fh_parm_offset = 0;
  1771. info = kzalloc(sizeof(struct fc_bsg_info), GFP_KERNEL);
  1772. if (!info) {
  1773. fc_frame_free(fp);
  1774. return -ENOMEM;
  1775. }
  1776. info->job = job;
  1777. info->lport = lport;
  1778. info->rsp_code = ELS_LS_ACC;
  1779. info->nents = job->reply_payload.sg_cnt;
  1780. info->sg = job->reply_payload.sg_list;
  1781. if (!lport->tt.exch_seq_send(lport, fp, fc_lport_bsg_resp,
  1782. NULL, info, tov)) {
  1783. kfree(info);
  1784. return -ECOMM;
  1785. }
  1786. return 0;
  1787. }
  1788. /**
  1789. * fc_lport_ct_request() - Send CT Passthrough request
  1790. * @job: The BSG Passthrough job
  1791. * @lport: The local port sending the request
  1792. * @did: The destination FC-ID
  1793. * @tov: The timeout period to wait for the response
  1794. *
  1795. * Locking Note: The lport lock is expected to be held before calling
  1796. * this routine.
  1797. */
  1798. static int fc_lport_ct_request(struct fc_bsg_job *job,
  1799. struct fc_lport *lport, u32 did, u32 tov)
  1800. {
  1801. struct fc_bsg_info *info;
  1802. struct fc_frame *fp;
  1803. struct fc_frame_header *fh;
  1804. struct fc_ct_req *ct;
  1805. size_t len;
  1806. fp = fc_frame_alloc(lport, sizeof(struct fc_ct_hdr) +
  1807. job->request_payload.payload_len);
  1808. if (!fp)
  1809. return -ENOMEM;
  1810. len = job->request_payload.payload_len;
  1811. ct = fc_frame_payload_get(fp, len);
  1812. sg_copy_to_buffer(job->request_payload.sg_list,
  1813. job->request_payload.sg_cnt,
  1814. ct, len);
  1815. fh = fc_frame_header_get(fp);
  1816. fh->fh_r_ctl = FC_RCTL_DD_UNSOL_CTL;
  1817. hton24(fh->fh_d_id, did);
  1818. hton24(fh->fh_s_id, lport->port_id);
  1819. fh->fh_type = FC_TYPE_CT;
  1820. hton24(fh->fh_f_ctl, FC_FCTL_REQ);
  1821. fh->fh_cs_ctl = 0;
  1822. fh->fh_df_ctl = 0;
  1823. fh->fh_parm_offset = 0;
  1824. info = kzalloc(sizeof(struct fc_bsg_info), GFP_KERNEL);
  1825. if (!info) {
  1826. fc_frame_free(fp);
  1827. return -ENOMEM;
  1828. }
  1829. info->job = job;
  1830. info->lport = lport;
  1831. info->rsp_code = FC_FS_ACC;
  1832. info->nents = job->reply_payload.sg_cnt;
  1833. info->sg = job->reply_payload.sg_list;
  1834. if (!lport->tt.exch_seq_send(lport, fp, fc_lport_bsg_resp,
  1835. NULL, info, tov)) {
  1836. kfree(info);
  1837. return -ECOMM;
  1838. }
  1839. return 0;
  1840. }
  1841. /**
  1842. * fc_lport_bsg_request() - The common entry point for sending
  1843. * FC Passthrough requests
  1844. * @job: The BSG passthrough job
  1845. */
  1846. int fc_lport_bsg_request(struct fc_bsg_job *job)
  1847. {
  1848. struct request *rsp = job->req->next_rq;
  1849. struct Scsi_Host *shost = job->shost;
  1850. struct fc_lport *lport = shost_priv(shost);
  1851. struct fc_rport *rport;
  1852. struct fc_rport_priv *rdata;
  1853. int rc = -EINVAL;
  1854. u32 did;
  1855. job->reply->reply_payload_rcv_len = 0;
  1856. if (rsp)
  1857. rsp->resid_len = job->reply_payload.payload_len;
  1858. mutex_lock(&lport->lp_mutex);
  1859. switch (job->request->msgcode) {
  1860. case FC_BSG_RPT_ELS:
  1861. rport = job->rport;
  1862. if (!rport)
  1863. break;
  1864. rdata = rport->dd_data;
  1865. rc = fc_lport_els_request(job, lport, rport->port_id,
  1866. rdata->e_d_tov);
  1867. break;
  1868. case FC_BSG_RPT_CT:
  1869. rport = job->rport;
  1870. if (!rport)
  1871. break;
  1872. rdata = rport->dd_data;
  1873. rc = fc_lport_ct_request(job, lport, rport->port_id,
  1874. rdata->e_d_tov);
  1875. break;
  1876. case FC_BSG_HST_CT:
  1877. did = ntoh24(job->request->rqst_data.h_ct.port_id);
  1878. if (did == FC_FID_DIR_SERV)
  1879. rdata = lport->dns_rdata;
  1880. else
  1881. rdata = lport->tt.rport_lookup(lport, did);
  1882. if (!rdata)
  1883. break;
  1884. rc = fc_lport_ct_request(job, lport, did, rdata->e_d_tov);
  1885. break;
  1886. case FC_BSG_HST_ELS_NOLOGIN:
  1887. did = ntoh24(job->request->rqst_data.h_els.port_id);
  1888. rc = fc_lport_els_request(job, lport, did, lport->e_d_tov);
  1889. break;
  1890. }
  1891. mutex_unlock(&lport->lp_mutex);
  1892. return rc;
  1893. }
  1894. EXPORT_SYMBOL(fc_lport_bsg_request);