fc_lport.c 49 KB

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