fc_lport.c 50 KB

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