fc_lport.c 50 KB

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