fc_lport.c 55 KB

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