fc_lport.c 48 KB

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