fc_lport.c 49 KB

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