fc_lport.c 49 KB

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