fc_disc.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729
  1. /*
  2. * Copyright(c) 2007 - 2008 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. * Target Discovery
  21. *
  22. * This block discovers all FC-4 remote ports, including FCP initiators. It
  23. * also handles RSCN events and re-discovery if necessary.
  24. */
  25. /*
  26. * DISC LOCKING
  27. *
  28. * The disc mutex is can be locked when acquiring rport locks, but may not
  29. * be held when acquiring the lport lock. Refer to fc_lport.c for more
  30. * details.
  31. */
  32. #include <linux/timer.h>
  33. #include <linux/err.h>
  34. #include <asm/unaligned.h>
  35. #include <scsi/fc/fc_gs.h>
  36. #include <scsi/libfc.h>
  37. #define FC_DISC_RETRY_LIMIT 3 /* max retries */
  38. #define FC_DISC_RETRY_DELAY 500UL /* (msecs) delay */
  39. static void fc_disc_gpn_ft_req(struct fc_disc *);
  40. static void fc_disc_gpn_ft_resp(struct fc_seq *, struct fc_frame *, void *);
  41. static int fc_disc_new_target(struct fc_disc *, struct fc_rport_priv *,
  42. struct fc_rport_identifiers *);
  43. static void fc_disc_done(struct fc_disc *, enum fc_disc_event);
  44. static void fc_disc_timeout(struct work_struct *);
  45. static void fc_disc_single(struct fc_disc *, struct fc_disc_port *);
  46. static void fc_disc_restart(struct fc_disc *);
  47. /**
  48. * fc_disc_stop_rports() - delete all the remote ports associated with the lport
  49. * @disc: The discovery job to stop rports on
  50. *
  51. * Locking Note: This function expects that the lport mutex is locked before
  52. * calling it.
  53. */
  54. void fc_disc_stop_rports(struct fc_disc *disc)
  55. {
  56. struct fc_lport *lport;
  57. struct fc_rport_priv *rdata, *next;
  58. lport = disc->lport;
  59. mutex_lock(&disc->disc_mutex);
  60. list_for_each_entry_safe(rdata, next, &disc->rports, peers)
  61. lport->tt.rport_logoff(rdata);
  62. mutex_unlock(&disc->disc_mutex);
  63. }
  64. /**
  65. * fc_disc_recv_rscn_req() - Handle Registered State Change Notification (RSCN)
  66. * @sp: Current sequence of the RSCN exchange
  67. * @fp: RSCN Frame
  68. * @lport: Fibre Channel host port instance
  69. *
  70. * Locking Note: This function expects that the disc_mutex is locked
  71. * before it is called.
  72. */
  73. static void fc_disc_recv_rscn_req(struct fc_seq *sp, struct fc_frame *fp,
  74. struct fc_disc *disc)
  75. {
  76. struct fc_lport *lport;
  77. struct fc_rport_priv *rdata;
  78. struct fc_els_rscn *rp;
  79. struct fc_els_rscn_page *pp;
  80. struct fc_seq_els_data rjt_data;
  81. unsigned int len;
  82. int redisc = 0;
  83. enum fc_els_rscn_ev_qual ev_qual;
  84. enum fc_els_rscn_addr_fmt fmt;
  85. LIST_HEAD(disc_ports);
  86. struct fc_disc_port *dp, *next;
  87. lport = disc->lport;
  88. FC_DISC_DBG(disc, "Received an RSCN event\n");
  89. /* make sure the frame contains an RSCN message */
  90. rp = fc_frame_payload_get(fp, sizeof(*rp));
  91. if (!rp)
  92. goto reject;
  93. /* make sure the page length is as expected (4 bytes) */
  94. if (rp->rscn_page_len != sizeof(*pp))
  95. goto reject;
  96. /* get the RSCN payload length */
  97. len = ntohs(rp->rscn_plen);
  98. if (len < sizeof(*rp))
  99. goto reject;
  100. /* make sure the frame contains the expected payload */
  101. rp = fc_frame_payload_get(fp, len);
  102. if (!rp)
  103. goto reject;
  104. /* payload must be a multiple of the RSCN page size */
  105. len -= sizeof(*rp);
  106. if (len % sizeof(*pp))
  107. goto reject;
  108. for (pp = (void *)(rp + 1); len > 0; len -= sizeof(*pp), pp++) {
  109. ev_qual = pp->rscn_page_flags >> ELS_RSCN_EV_QUAL_BIT;
  110. ev_qual &= ELS_RSCN_EV_QUAL_MASK;
  111. fmt = pp->rscn_page_flags >> ELS_RSCN_ADDR_FMT_BIT;
  112. fmt &= ELS_RSCN_ADDR_FMT_MASK;
  113. /*
  114. * if we get an address format other than port
  115. * (area, domain, fabric), then do a full discovery
  116. */
  117. switch (fmt) {
  118. case ELS_ADDR_FMT_PORT:
  119. FC_DISC_DBG(disc, "Port address format for port "
  120. "(%6x)\n", ntoh24(pp->rscn_fid));
  121. dp = kzalloc(sizeof(*dp), GFP_KERNEL);
  122. if (!dp) {
  123. redisc = 1;
  124. break;
  125. }
  126. dp->lp = lport;
  127. dp->ids.port_id = ntoh24(pp->rscn_fid);
  128. dp->ids.port_name = -1;
  129. dp->ids.node_name = -1;
  130. dp->ids.roles = FC_RPORT_ROLE_UNKNOWN;
  131. list_add_tail(&dp->peers, &disc_ports);
  132. break;
  133. case ELS_ADDR_FMT_AREA:
  134. case ELS_ADDR_FMT_DOM:
  135. case ELS_ADDR_FMT_FAB:
  136. default:
  137. FC_DISC_DBG(disc, "Address format is (%d)\n", fmt);
  138. redisc = 1;
  139. break;
  140. }
  141. }
  142. lport->tt.seq_els_rsp_send(sp, ELS_LS_ACC, NULL);
  143. if (redisc) {
  144. FC_DISC_DBG(disc, "RSCN received: rediscovering\n");
  145. fc_disc_restart(disc);
  146. } else {
  147. FC_DISC_DBG(disc, "RSCN received: not rediscovering. "
  148. "redisc %d state %d in_prog %d\n",
  149. redisc, lport->state, disc->pending);
  150. list_for_each_entry_safe(dp, next, &disc_ports, peers) {
  151. list_del(&dp->peers);
  152. rdata = lport->tt.rport_lookup(lport, dp->ids.port_id);
  153. if (rdata) {
  154. lport->tt.rport_logoff(rdata);
  155. }
  156. fc_disc_single(disc, dp);
  157. }
  158. }
  159. fc_frame_free(fp);
  160. return;
  161. reject:
  162. FC_DISC_DBG(disc, "Received a bad RSCN frame\n");
  163. rjt_data.fp = NULL;
  164. rjt_data.reason = ELS_RJT_LOGIC;
  165. rjt_data.explan = ELS_EXPL_NONE;
  166. lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &rjt_data);
  167. fc_frame_free(fp);
  168. }
  169. /**
  170. * fc_disc_recv_req() - Handle incoming requests
  171. * @sp: Current sequence of the request exchange
  172. * @fp: The frame
  173. * @lport: The FC local port
  174. *
  175. * Locking Note: This function is called from the EM and will lock
  176. * the disc_mutex before calling the handler for the
  177. * request.
  178. */
  179. static void fc_disc_recv_req(struct fc_seq *sp, struct fc_frame *fp,
  180. struct fc_lport *lport)
  181. {
  182. u8 op;
  183. struct fc_disc *disc = &lport->disc;
  184. op = fc_frame_payload_op(fp);
  185. switch (op) {
  186. case ELS_RSCN:
  187. mutex_lock(&disc->disc_mutex);
  188. fc_disc_recv_rscn_req(sp, fp, disc);
  189. mutex_unlock(&disc->disc_mutex);
  190. break;
  191. default:
  192. FC_DISC_DBG(disc, "Received an unsupported request, "
  193. "the opcode is (%x)\n", op);
  194. break;
  195. }
  196. }
  197. /**
  198. * fc_disc_restart() - Restart discovery
  199. * @lport: FC discovery context
  200. *
  201. * Locking Note: This function expects that the disc mutex
  202. * is already locked.
  203. */
  204. static void fc_disc_restart(struct fc_disc *disc)
  205. {
  206. FC_DISC_DBG(disc, "Restarting discovery\n");
  207. disc->requested = 1;
  208. if (disc->pending)
  209. return;
  210. /*
  211. * Advance disc_id. This is an arbitrary non-zero number that will
  212. * match the value in the fc_rport_priv after discovery for all
  213. * freshly-discovered remote ports. Avoid wrapping to zero.
  214. */
  215. disc->disc_id = (disc->disc_id + 2) | 1;
  216. disc->retry_count = 0;
  217. fc_disc_gpn_ft_req(disc);
  218. }
  219. /**
  220. * fc_disc_start() - Fibre Channel Target discovery
  221. * @lport: FC local port
  222. *
  223. * Returns non-zero if discovery cannot be started.
  224. */
  225. static void fc_disc_start(void (*disc_callback)(struct fc_lport *,
  226. enum fc_disc_event),
  227. struct fc_lport *lport)
  228. {
  229. struct fc_rport_priv *rdata;
  230. struct fc_disc *disc = &lport->disc;
  231. /*
  232. * At this point we may have a new disc job or an existing
  233. * one. Either way, let's lock when we make changes to it
  234. * and send the GPN_FT request.
  235. */
  236. mutex_lock(&disc->disc_mutex);
  237. disc->disc_callback = disc_callback;
  238. /*
  239. * If not ready, or already running discovery, just set request flag.
  240. */
  241. disc->requested = 1;
  242. if (disc->pending) {
  243. mutex_unlock(&disc->disc_mutex);
  244. return;
  245. }
  246. /*
  247. * Handle point-to-point mode as a simple discovery
  248. * of the remote port. Yucky, yucky, yuck, yuck!
  249. */
  250. rdata = disc->lport->ptp_rp;
  251. if (rdata) {
  252. kref_get(&rdata->kref);
  253. if (!fc_disc_new_target(disc, rdata, &rdata->ids)) {
  254. fc_disc_done(disc, DISC_EV_SUCCESS);
  255. }
  256. kref_put(&rdata->kref, rdata->local_port->tt.rport_destroy);
  257. } else {
  258. disc->disc_id = (disc->disc_id + 2) | 1;
  259. fc_disc_gpn_ft_req(disc); /* get ports by FC-4 type */
  260. }
  261. mutex_unlock(&disc->disc_mutex);
  262. }
  263. /**
  264. * fc_disc_new_target() - Handle new target found by discovery
  265. * @lport: FC local port
  266. * @rdata: The previous FC remote port priv (NULL if new remote port)
  267. * @ids: Identifiers for the new FC remote port
  268. *
  269. * Locking Note: This function expects that the disc_mutex is locked
  270. * before it is called.
  271. */
  272. static int fc_disc_new_target(struct fc_disc *disc,
  273. struct fc_rport_priv *rdata,
  274. struct fc_rport_identifiers *ids)
  275. {
  276. struct fc_lport *lport = disc->lport;
  277. int error = 0;
  278. if (rdata && ids->port_name) {
  279. if (rdata->ids.port_name == -1) {
  280. /*
  281. * Set WWN and fall through to notify of create.
  282. */
  283. rdata->ids.port_name = ids->port_name;
  284. rdata->ids.node_name = ids->node_name;
  285. } else if (rdata->ids.port_name != ids->port_name) {
  286. /*
  287. * This is a new port with the same FCID as
  288. * a previously-discovered port. Presumably the old
  289. * port logged out and a new port logged in and was
  290. * assigned the same FCID. This should be rare.
  291. * Delete the old one and fall thru to re-create.
  292. */
  293. lport->tt.rport_logoff(rdata);
  294. rdata = NULL;
  295. }
  296. }
  297. if (((ids->port_name != -1) || (ids->port_id != -1)) &&
  298. ids->port_id != fc_host_port_id(lport->host) &&
  299. ids->port_name != lport->wwpn) {
  300. if (!rdata) {
  301. rdata = lport->tt.rport_create(lport, ids);
  302. if (!rdata)
  303. error = -ENOMEM;
  304. }
  305. if (rdata)
  306. lport->tt.rport_login(rdata);
  307. }
  308. return error;
  309. }
  310. /**
  311. * fc_disc_done() - Discovery has been completed
  312. * @disc: FC discovery context
  313. * @event: discovery completion status
  314. *
  315. * Locking Note: This function expects that the disc mutex is locked before
  316. * it is called. The discovery callback is then made with the lock released,
  317. * and the lock is re-taken before returning from this function
  318. */
  319. static void fc_disc_done(struct fc_disc *disc, enum fc_disc_event event)
  320. {
  321. struct fc_lport *lport = disc->lport;
  322. struct fc_rport_priv *rdata;
  323. FC_DISC_DBG(disc, "Discovery complete\n");
  324. disc->pending = 0;
  325. if (disc->requested) {
  326. fc_disc_restart(disc);
  327. return;
  328. }
  329. /*
  330. * Go through all remote ports. If they were found in the latest
  331. * discovery, reverify or log them in. Otherwise, log them out.
  332. * Skip ports which were never discovered. These are the dNS port
  333. * and ports which were created by PLOGI.
  334. */
  335. list_for_each_entry(rdata, &disc->rports, peers) {
  336. if (!rdata->disc_id)
  337. continue;
  338. if (rdata->disc_id == disc->disc_id)
  339. lport->tt.rport_login(rdata);
  340. else
  341. lport->tt.rport_logoff(rdata);
  342. }
  343. mutex_unlock(&disc->disc_mutex);
  344. disc->disc_callback(lport, event);
  345. mutex_lock(&disc->disc_mutex);
  346. }
  347. /**
  348. * fc_disc_error() - Handle error on dNS request
  349. * @disc: FC discovery context
  350. * @fp: The frame pointer
  351. */
  352. static void fc_disc_error(struct fc_disc *disc, struct fc_frame *fp)
  353. {
  354. struct fc_lport *lport = disc->lport;
  355. unsigned long delay = 0;
  356. FC_DISC_DBG(disc, "Error %ld, retries %d/%d\n",
  357. PTR_ERR(fp), disc->retry_count,
  358. FC_DISC_RETRY_LIMIT);
  359. if (!fp || PTR_ERR(fp) == -FC_EX_TIMEOUT) {
  360. /*
  361. * Memory allocation failure, or the exchange timed out,
  362. * retry after delay.
  363. */
  364. if (disc->retry_count < FC_DISC_RETRY_LIMIT) {
  365. /* go ahead and retry */
  366. if (!fp)
  367. delay = msecs_to_jiffies(FC_DISC_RETRY_DELAY);
  368. else {
  369. delay = msecs_to_jiffies(lport->e_d_tov);
  370. /* timeout faster first time */
  371. if (!disc->retry_count)
  372. delay /= 4;
  373. }
  374. disc->retry_count++;
  375. schedule_delayed_work(&disc->disc_work, delay);
  376. } else
  377. fc_disc_done(disc, DISC_EV_FAILED);
  378. }
  379. }
  380. /**
  381. * fc_disc_gpn_ft_req() - Send Get Port Names by FC-4 type (GPN_FT) request
  382. * @lport: FC discovery context
  383. *
  384. * Locking Note: This function expects that the disc_mutex is locked
  385. * before it is called.
  386. */
  387. static void fc_disc_gpn_ft_req(struct fc_disc *disc)
  388. {
  389. struct fc_frame *fp;
  390. struct fc_lport *lport = disc->lport;
  391. WARN_ON(!fc_lport_test_ready(lport));
  392. disc->pending = 1;
  393. disc->requested = 0;
  394. disc->buf_len = 0;
  395. disc->seq_count = 0;
  396. fp = fc_frame_alloc(lport,
  397. sizeof(struct fc_ct_hdr) +
  398. sizeof(struct fc_ns_gid_ft));
  399. if (!fp)
  400. goto err;
  401. if (lport->tt.elsct_send(lport, 0, fp,
  402. FC_NS_GPN_FT,
  403. fc_disc_gpn_ft_resp,
  404. disc, lport->e_d_tov))
  405. return;
  406. err:
  407. fc_disc_error(disc, fp);
  408. }
  409. /**
  410. * fc_disc_gpn_ft_parse() - Parse the body of the dNS GPN_FT response.
  411. * @lport: Fibre Channel host port instance
  412. * @buf: GPN_FT response buffer
  413. * @len: size of response buffer
  414. *
  415. * Goes through the list of IDs and names resulting from a request.
  416. */
  417. static int fc_disc_gpn_ft_parse(struct fc_disc *disc, void *buf, size_t len)
  418. {
  419. struct fc_lport *lport;
  420. struct fc_gpn_ft_resp *np;
  421. char *bp;
  422. size_t plen;
  423. size_t tlen;
  424. int error = 0;
  425. struct fc_rport_identifiers ids;
  426. struct fc_rport_priv *rdata;
  427. lport = disc->lport;
  428. disc->seq_count++;
  429. /*
  430. * Handle partial name record left over from previous call.
  431. */
  432. bp = buf;
  433. plen = len;
  434. np = (struct fc_gpn_ft_resp *)bp;
  435. tlen = disc->buf_len;
  436. if (tlen) {
  437. WARN_ON(tlen >= sizeof(*np));
  438. plen = sizeof(*np) - tlen;
  439. WARN_ON(plen <= 0);
  440. WARN_ON(plen >= sizeof(*np));
  441. if (plen > len)
  442. plen = len;
  443. np = &disc->partial_buf;
  444. memcpy((char *)np + tlen, bp, plen);
  445. /*
  446. * Set bp so that the loop below will advance it to the
  447. * first valid full name element.
  448. */
  449. bp -= tlen;
  450. len += tlen;
  451. plen += tlen;
  452. disc->buf_len = (unsigned char) plen;
  453. if (plen == sizeof(*np))
  454. disc->buf_len = 0;
  455. }
  456. /*
  457. * Handle full name records, including the one filled from above.
  458. * Normally, np == bp and plen == len, but from the partial case above,
  459. * bp, len describe the overall buffer, and np, plen describe the
  460. * partial buffer, which if would usually be full now.
  461. * After the first time through the loop, things return to "normal".
  462. */
  463. while (plen >= sizeof(*np)) {
  464. ids.port_id = ntoh24(np->fp_fid);
  465. ids.port_name = ntohll(np->fp_wwpn);
  466. ids.node_name = -1;
  467. ids.roles = FC_RPORT_ROLE_UNKNOWN;
  468. if (ids.port_id != fc_host_port_id(lport->host) &&
  469. ids.port_name != lport->wwpn) {
  470. rdata = lport->tt.rport_create(lport, &ids);
  471. if (rdata)
  472. rdata->disc_id = disc->disc_id;
  473. else
  474. printk(KERN_WARNING "libfc: Failed to allocate "
  475. "memory for the newly discovered port "
  476. "(%6x)\n", ids.port_id);
  477. }
  478. if (np->fp_flags & FC_NS_FID_LAST) {
  479. fc_disc_done(disc, DISC_EV_SUCCESS);
  480. len = 0;
  481. break;
  482. }
  483. len -= sizeof(*np);
  484. bp += sizeof(*np);
  485. np = (struct fc_gpn_ft_resp *)bp;
  486. plen = len;
  487. }
  488. /*
  489. * Save any partial record at the end of the buffer for next time.
  490. */
  491. if (error == 0 && len > 0 && len < sizeof(*np)) {
  492. if (np != &disc->partial_buf) {
  493. FC_DISC_DBG(disc, "Partial buffer remains "
  494. "for discovery\n");
  495. memcpy(&disc->partial_buf, np, len);
  496. }
  497. disc->buf_len = (unsigned char) len;
  498. } else {
  499. disc->buf_len = 0;
  500. }
  501. return error;
  502. }
  503. /**
  504. * fc_disc_timeout() - Retry handler for the disc component
  505. * @work: Structure holding disc obj that needs retry discovery
  506. *
  507. * Handle retry of memory allocation for remote ports.
  508. */
  509. static void fc_disc_timeout(struct work_struct *work)
  510. {
  511. struct fc_disc *disc = container_of(work,
  512. struct fc_disc,
  513. disc_work.work);
  514. mutex_lock(&disc->disc_mutex);
  515. fc_disc_gpn_ft_req(disc);
  516. mutex_unlock(&disc->disc_mutex);
  517. }
  518. /**
  519. * fc_disc_gpn_ft_resp() - Handle a response frame from Get Port Names (GPN_FT)
  520. * @sp: Current sequence of GPN_FT exchange
  521. * @fp: response frame
  522. * @lp_arg: Fibre Channel host port instance
  523. *
  524. * Locking Note: This function is called without disc mutex held, and
  525. * should do all its processing with the mutex held
  526. */
  527. static void fc_disc_gpn_ft_resp(struct fc_seq *sp, struct fc_frame *fp,
  528. void *disc_arg)
  529. {
  530. struct fc_disc *disc = disc_arg;
  531. struct fc_ct_hdr *cp;
  532. struct fc_frame_header *fh;
  533. enum fc_disc_event event = DISC_EV_NONE;
  534. unsigned int seq_cnt;
  535. unsigned int len;
  536. int error = 0;
  537. mutex_lock(&disc->disc_mutex);
  538. FC_DISC_DBG(disc, "Received a GPN_FT response\n");
  539. if (IS_ERR(fp)) {
  540. fc_disc_error(disc, fp);
  541. mutex_unlock(&disc->disc_mutex);
  542. return;
  543. }
  544. WARN_ON(!fc_frame_is_linear(fp)); /* buffer must be contiguous */
  545. fh = fc_frame_header_get(fp);
  546. len = fr_len(fp) - sizeof(*fh);
  547. seq_cnt = ntohs(fh->fh_seq_cnt);
  548. if (fr_sof(fp) == FC_SOF_I3 && seq_cnt == 0 && disc->seq_count == 0) {
  549. cp = fc_frame_payload_get(fp, sizeof(*cp));
  550. if (!cp) {
  551. FC_DISC_DBG(disc, "GPN_FT response too short, len %d\n",
  552. fr_len(fp));
  553. event = DISC_EV_FAILED;
  554. } else if (ntohs(cp->ct_cmd) == FC_FS_ACC) {
  555. /* Accepted, parse the response. */
  556. len -= sizeof(*cp);
  557. error = fc_disc_gpn_ft_parse(disc, cp + 1, len);
  558. } else if (ntohs(cp->ct_cmd) == FC_FS_RJT) {
  559. FC_DISC_DBG(disc, "GPN_FT rejected reason %x exp %x "
  560. "(check zoning)\n", cp->ct_reason,
  561. cp->ct_explan);
  562. event = DISC_EV_FAILED;
  563. if (cp->ct_reason == FC_FS_RJT_UNABL &&
  564. cp->ct_explan == FC_FS_EXP_FTNR)
  565. event = DISC_EV_SUCCESS;
  566. } else {
  567. FC_DISC_DBG(disc, "GPN_FT unexpected response code "
  568. "%x\n", ntohs(cp->ct_cmd));
  569. event = DISC_EV_FAILED;
  570. }
  571. } else if (fr_sof(fp) == FC_SOF_N3 && seq_cnt == disc->seq_count) {
  572. error = fc_disc_gpn_ft_parse(disc, fh + 1, len);
  573. } else {
  574. FC_DISC_DBG(disc, "GPN_FT unexpected frame - out of sequence? "
  575. "seq_cnt %x expected %x sof %x eof %x\n",
  576. seq_cnt, disc->seq_count, fr_sof(fp), fr_eof(fp));
  577. event = DISC_EV_FAILED;
  578. }
  579. if (error)
  580. fc_disc_error(disc, fp);
  581. else if (event != DISC_EV_NONE)
  582. fc_disc_done(disc, event);
  583. fc_frame_free(fp);
  584. mutex_unlock(&disc->disc_mutex);
  585. }
  586. /**
  587. * fc_disc_single() - Discover the directory information for a single target
  588. * @lport: FC local port
  589. * @dp: The port to rediscover
  590. *
  591. * Locking Note: This function expects that the disc_mutex is locked
  592. * before it is called.
  593. */
  594. static void fc_disc_single(struct fc_disc *disc, struct fc_disc_port *dp)
  595. {
  596. struct fc_lport *lport;
  597. struct fc_rport_priv *rdata;
  598. lport = disc->lport;
  599. if (dp->ids.port_id == fc_host_port_id(lport->host))
  600. goto out;
  601. rdata = lport->tt.rport_create(lport, &dp->ids);
  602. if (rdata) {
  603. rdata->disc_id = disc->disc_id;
  604. kfree(dp);
  605. lport->tt.rport_login(rdata);
  606. }
  607. return;
  608. out:
  609. kfree(dp);
  610. }
  611. /**
  612. * fc_disc_stop() - Stop discovery for a given lport
  613. * @lport: The lport that discovery should stop for
  614. */
  615. void fc_disc_stop(struct fc_lport *lport)
  616. {
  617. struct fc_disc *disc = &lport->disc;
  618. if (disc) {
  619. cancel_delayed_work_sync(&disc->disc_work);
  620. fc_disc_stop_rports(disc);
  621. }
  622. }
  623. /**
  624. * fc_disc_stop_final() - Stop discovery for a given lport
  625. * @lport: The lport that discovery should stop for
  626. *
  627. * This function will block until discovery has been
  628. * completely stopped and all rports have been deleted.
  629. */
  630. void fc_disc_stop_final(struct fc_lport *lport)
  631. {
  632. fc_disc_stop(lport);
  633. lport->tt.rport_flush_queue();
  634. }
  635. /**
  636. * fc_disc_init() - Initialize the discovery block
  637. * @lport: FC local port
  638. */
  639. int fc_disc_init(struct fc_lport *lport)
  640. {
  641. struct fc_disc *disc;
  642. if (!lport->tt.disc_start)
  643. lport->tt.disc_start = fc_disc_start;
  644. if (!lport->tt.disc_stop)
  645. lport->tt.disc_stop = fc_disc_stop;
  646. if (!lport->tt.disc_stop_final)
  647. lport->tt.disc_stop_final = fc_disc_stop_final;
  648. if (!lport->tt.disc_recv_req)
  649. lport->tt.disc_recv_req = fc_disc_recv_req;
  650. disc = &lport->disc;
  651. INIT_DELAYED_WORK(&disc->disc_work, fc_disc_timeout);
  652. mutex_init(&disc->disc_mutex);
  653. INIT_LIST_HEAD(&disc->rports);
  654. disc->lport = lport;
  655. return 0;
  656. }
  657. EXPORT_SYMBOL(fc_disc_init);