fc_disc.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707
  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. struct fc_rport_priv *rdata, *next;
  207. struct fc_lport *lport = disc->lport;
  208. FC_DISC_DBG(disc, "Restarting discovery\n");
  209. list_for_each_entry_safe(rdata, next, &disc->rports, peers)
  210. lport->tt.rport_logoff(rdata);
  211. disc->requested = 1;
  212. if (!disc->pending)
  213. fc_disc_gpn_ft_req(disc);
  214. }
  215. /**
  216. * fc_disc_start() - Fibre Channel Target discovery
  217. * @lport: FC local port
  218. *
  219. * Returns non-zero if discovery cannot be started.
  220. */
  221. static void fc_disc_start(void (*disc_callback)(struct fc_lport *,
  222. enum fc_disc_event),
  223. struct fc_lport *lport)
  224. {
  225. struct fc_rport_priv *rdata;
  226. struct fc_disc *disc = &lport->disc;
  227. /*
  228. * At this point we may have a new disc job or an existing
  229. * one. Either way, let's lock when we make changes to it
  230. * and send the GPN_FT request.
  231. */
  232. mutex_lock(&disc->disc_mutex);
  233. disc->disc_callback = disc_callback;
  234. /*
  235. * If not ready, or already running discovery, just set request flag.
  236. */
  237. disc->requested = 1;
  238. if (disc->pending) {
  239. mutex_unlock(&disc->disc_mutex);
  240. return;
  241. }
  242. /*
  243. * Handle point-to-point mode as a simple discovery
  244. * of the remote port. Yucky, yucky, yuck, yuck!
  245. */
  246. rdata = disc->lport->ptp_rp;
  247. if (rdata) {
  248. kref_get(&rdata->kref);
  249. if (!fc_disc_new_target(disc, rdata, &rdata->ids)) {
  250. fc_disc_done(disc, DISC_EV_SUCCESS);
  251. }
  252. kref_put(&rdata->kref, rdata->local_port->tt.rport_destroy);
  253. } else {
  254. fc_disc_gpn_ft_req(disc); /* get ports by FC-4 type */
  255. }
  256. mutex_unlock(&disc->disc_mutex);
  257. }
  258. /**
  259. * fc_disc_new_target() - Handle new target found by discovery
  260. * @lport: FC local port
  261. * @rdata: The previous FC remote port priv (NULL if new remote port)
  262. * @ids: Identifiers for the new FC remote port
  263. *
  264. * Locking Note: This function expects that the disc_mutex is locked
  265. * before it is called.
  266. */
  267. static int fc_disc_new_target(struct fc_disc *disc,
  268. struct fc_rport_priv *rdata,
  269. struct fc_rport_identifiers *ids)
  270. {
  271. struct fc_lport *lport = disc->lport;
  272. int error = 0;
  273. if (rdata && ids->port_name) {
  274. if (rdata->ids.port_name == -1) {
  275. /*
  276. * Set WWN and fall through to notify of create.
  277. */
  278. rdata->ids.port_name = ids->port_name;
  279. rdata->ids.node_name = ids->node_name;
  280. } else if (rdata->ids.port_name != ids->port_name) {
  281. /*
  282. * This is a new port with the same FCID as
  283. * a previously-discovered port. Presumably the old
  284. * port logged out and a new port logged in and was
  285. * assigned the same FCID. This should be rare.
  286. * Delete the old one and fall thru to re-create.
  287. */
  288. lport->tt.rport_logoff(rdata);
  289. rdata = NULL;
  290. }
  291. }
  292. if (((ids->port_name != -1) || (ids->port_id != -1)) &&
  293. ids->port_id != fc_host_port_id(lport->host) &&
  294. ids->port_name != lport->wwpn) {
  295. if (!rdata) {
  296. rdata = lport->tt.rport_create(lport, ids);
  297. if (!rdata)
  298. error = -ENOMEM;
  299. }
  300. if (rdata)
  301. lport->tt.rport_login(rdata);
  302. }
  303. return error;
  304. }
  305. /**
  306. * fc_disc_done() - Discovery has been completed
  307. * @disc: FC discovery context
  308. * @event: discovery completion status
  309. *
  310. * Locking Note: This function expects that the disc mutex is locked before
  311. * it is called. The discovery callback is then made with the lock released,
  312. * and the lock is re-taken before returning from this function
  313. */
  314. static void fc_disc_done(struct fc_disc *disc, enum fc_disc_event event)
  315. {
  316. struct fc_lport *lport = disc->lport;
  317. FC_DISC_DBG(disc, "Discovery complete\n");
  318. if (disc->requested)
  319. fc_disc_gpn_ft_req(disc);
  320. else
  321. disc->pending = 0;
  322. mutex_unlock(&disc->disc_mutex);
  323. disc->disc_callback(lport, event);
  324. mutex_lock(&disc->disc_mutex);
  325. }
  326. /**
  327. * fc_disc_error() - Handle error on dNS request
  328. * @disc: FC discovery context
  329. * @fp: The frame pointer
  330. */
  331. static void fc_disc_error(struct fc_disc *disc, struct fc_frame *fp)
  332. {
  333. struct fc_lport *lport = disc->lport;
  334. unsigned long delay = 0;
  335. FC_DISC_DBG(disc, "Error %ld, retries %d/%d\n",
  336. PTR_ERR(fp), disc->retry_count,
  337. FC_DISC_RETRY_LIMIT);
  338. if (!fp || PTR_ERR(fp) == -FC_EX_TIMEOUT) {
  339. /*
  340. * Memory allocation failure, or the exchange timed out,
  341. * retry after delay.
  342. */
  343. if (disc->retry_count < FC_DISC_RETRY_LIMIT) {
  344. /* go ahead and retry */
  345. if (!fp)
  346. delay = msecs_to_jiffies(FC_DISC_RETRY_DELAY);
  347. else {
  348. delay = msecs_to_jiffies(lport->e_d_tov);
  349. /* timeout faster first time */
  350. if (!disc->retry_count)
  351. delay /= 4;
  352. }
  353. disc->retry_count++;
  354. schedule_delayed_work(&disc->disc_work, delay);
  355. } else
  356. fc_disc_done(disc, DISC_EV_FAILED);
  357. }
  358. }
  359. /**
  360. * fc_disc_gpn_ft_req() - Send Get Port Names by FC-4 type (GPN_FT) request
  361. * @lport: FC discovery context
  362. *
  363. * Locking Note: This function expects that the disc_mutex is locked
  364. * before it is called.
  365. */
  366. static void fc_disc_gpn_ft_req(struct fc_disc *disc)
  367. {
  368. struct fc_frame *fp;
  369. struct fc_lport *lport = disc->lport;
  370. WARN_ON(!fc_lport_test_ready(lport));
  371. disc->pending = 1;
  372. disc->requested = 0;
  373. disc->buf_len = 0;
  374. disc->seq_count = 0;
  375. fp = fc_frame_alloc(lport,
  376. sizeof(struct fc_ct_hdr) +
  377. sizeof(struct fc_ns_gid_ft));
  378. if (!fp)
  379. goto err;
  380. if (lport->tt.elsct_send(lport, 0, fp,
  381. FC_NS_GPN_FT,
  382. fc_disc_gpn_ft_resp,
  383. disc, lport->e_d_tov))
  384. return;
  385. err:
  386. fc_disc_error(disc, fp);
  387. }
  388. /**
  389. * fc_disc_gpn_ft_parse() - Parse the body of the dNS GPN_FT response.
  390. * @lport: Fibre Channel host port instance
  391. * @buf: GPN_FT response buffer
  392. * @len: size of response buffer
  393. *
  394. * Goes through the list of IDs and names resulting from a request.
  395. */
  396. static int fc_disc_gpn_ft_parse(struct fc_disc *disc, void *buf, size_t len)
  397. {
  398. struct fc_lport *lport;
  399. struct fc_gpn_ft_resp *np;
  400. char *bp;
  401. size_t plen;
  402. size_t tlen;
  403. int error = 0;
  404. struct fc_rport_identifiers ids;
  405. struct fc_rport_priv *rdata;
  406. lport = disc->lport;
  407. /*
  408. * Handle partial name record left over from previous call.
  409. */
  410. bp = buf;
  411. plen = len;
  412. np = (struct fc_gpn_ft_resp *)bp;
  413. tlen = disc->buf_len;
  414. if (tlen) {
  415. WARN_ON(tlen >= sizeof(*np));
  416. plen = sizeof(*np) - tlen;
  417. WARN_ON(plen <= 0);
  418. WARN_ON(plen >= sizeof(*np));
  419. if (plen > len)
  420. plen = len;
  421. np = &disc->partial_buf;
  422. memcpy((char *)np + tlen, bp, plen);
  423. /*
  424. * Set bp so that the loop below will advance it to the
  425. * first valid full name element.
  426. */
  427. bp -= tlen;
  428. len += tlen;
  429. plen += tlen;
  430. disc->buf_len = (unsigned char) plen;
  431. if (plen == sizeof(*np))
  432. disc->buf_len = 0;
  433. }
  434. /*
  435. * Handle full name records, including the one filled from above.
  436. * Normally, np == bp and plen == len, but from the partial case above,
  437. * bp, len describe the overall buffer, and np, plen describe the
  438. * partial buffer, which if would usually be full now.
  439. * After the first time through the loop, things return to "normal".
  440. */
  441. while (plen >= sizeof(*np)) {
  442. ids.port_id = ntoh24(np->fp_fid);
  443. ids.port_name = ntohll(np->fp_wwpn);
  444. ids.node_name = -1;
  445. ids.roles = FC_RPORT_ROLE_UNKNOWN;
  446. if (ids.port_id != fc_host_port_id(lport->host) &&
  447. ids.port_name != lport->wwpn) {
  448. rdata = lport->tt.rport_create(lport, &ids);
  449. if (rdata)
  450. lport->tt.rport_login(rdata);
  451. else
  452. printk(KERN_WARNING "libfc: Failed to allocate "
  453. "memory for the newly discovered port "
  454. "(%6x)\n", ids.port_id);
  455. }
  456. if (np->fp_flags & FC_NS_FID_LAST) {
  457. fc_disc_done(disc, DISC_EV_SUCCESS);
  458. len = 0;
  459. break;
  460. }
  461. len -= sizeof(*np);
  462. bp += sizeof(*np);
  463. np = (struct fc_gpn_ft_resp *)bp;
  464. plen = len;
  465. }
  466. /*
  467. * Save any partial record at the end of the buffer for next time.
  468. */
  469. if (error == 0 && len > 0 && len < sizeof(*np)) {
  470. if (np != &disc->partial_buf) {
  471. FC_DISC_DBG(disc, "Partial buffer remains "
  472. "for discovery\n");
  473. memcpy(&disc->partial_buf, np, len);
  474. }
  475. disc->buf_len = (unsigned char) len;
  476. } else {
  477. disc->buf_len = 0;
  478. }
  479. return error;
  480. }
  481. /**
  482. * fc_disc_timeout() - Retry handler for the disc component
  483. * @work: Structure holding disc obj that needs retry discovery
  484. *
  485. * Handle retry of memory allocation for remote ports.
  486. */
  487. static void fc_disc_timeout(struct work_struct *work)
  488. {
  489. struct fc_disc *disc = container_of(work,
  490. struct fc_disc,
  491. disc_work.work);
  492. mutex_lock(&disc->disc_mutex);
  493. if (disc->requested && !disc->pending)
  494. fc_disc_gpn_ft_req(disc);
  495. mutex_unlock(&disc->disc_mutex);
  496. }
  497. /**
  498. * fc_disc_gpn_ft_resp() - Handle a response frame from Get Port Names (GPN_FT)
  499. * @sp: Current sequence of GPN_FT exchange
  500. * @fp: response frame
  501. * @lp_arg: Fibre Channel host port instance
  502. *
  503. * Locking Note: This function is called without disc mutex held, and
  504. * should do all its processing with the mutex held
  505. */
  506. static void fc_disc_gpn_ft_resp(struct fc_seq *sp, struct fc_frame *fp,
  507. void *disc_arg)
  508. {
  509. struct fc_disc *disc = disc_arg;
  510. struct fc_ct_hdr *cp;
  511. struct fc_frame_header *fh;
  512. unsigned int seq_cnt;
  513. void *buf = NULL;
  514. unsigned int len;
  515. int error;
  516. mutex_lock(&disc->disc_mutex);
  517. FC_DISC_DBG(disc, "Received a GPN_FT response\n");
  518. if (IS_ERR(fp)) {
  519. fc_disc_error(disc, fp);
  520. mutex_unlock(&disc->disc_mutex);
  521. return;
  522. }
  523. WARN_ON(!fc_frame_is_linear(fp)); /* buffer must be contiguous */
  524. fh = fc_frame_header_get(fp);
  525. len = fr_len(fp) - sizeof(*fh);
  526. seq_cnt = ntohs(fh->fh_seq_cnt);
  527. if (fr_sof(fp) == FC_SOF_I3 && seq_cnt == 0 &&
  528. disc->seq_count == 0) {
  529. cp = fc_frame_payload_get(fp, sizeof(*cp));
  530. if (!cp) {
  531. FC_DISC_DBG(disc, "GPN_FT response too short, len %d\n",
  532. fr_len(fp));
  533. } else if (ntohs(cp->ct_cmd) == FC_FS_ACC) {
  534. /* Accepted, parse the response. */
  535. buf = cp + 1;
  536. len -= sizeof(*cp);
  537. } else if (ntohs(cp->ct_cmd) == FC_FS_RJT) {
  538. FC_DISC_DBG(disc, "GPN_FT rejected reason %x exp %x "
  539. "(check zoning)\n", cp->ct_reason,
  540. cp->ct_explan);
  541. fc_disc_done(disc, DISC_EV_FAILED);
  542. } else {
  543. FC_DISC_DBG(disc, "GPN_FT unexpected response code "
  544. "%x\n", ntohs(cp->ct_cmd));
  545. }
  546. } else if (fr_sof(fp) == FC_SOF_N3 &&
  547. seq_cnt == disc->seq_count) {
  548. buf = fh + 1;
  549. } else {
  550. FC_DISC_DBG(disc, "GPN_FT unexpected frame - out of sequence? "
  551. "seq_cnt %x expected %x sof %x eof %x\n",
  552. seq_cnt, disc->seq_count, fr_sof(fp), fr_eof(fp));
  553. }
  554. if (buf) {
  555. error = fc_disc_gpn_ft_parse(disc, buf, len);
  556. if (error)
  557. fc_disc_error(disc, fp);
  558. else
  559. disc->seq_count++;
  560. }
  561. fc_frame_free(fp);
  562. mutex_unlock(&disc->disc_mutex);
  563. }
  564. /**
  565. * fc_disc_single() - Discover the directory information for a single target
  566. * @lport: FC local port
  567. * @dp: The port to rediscover
  568. *
  569. * Locking Note: This function expects that the disc_mutex is locked
  570. * before it is called.
  571. */
  572. static void fc_disc_single(struct fc_disc *disc, struct fc_disc_port *dp)
  573. {
  574. struct fc_lport *lport;
  575. struct fc_rport_priv *rdata;
  576. lport = disc->lport;
  577. if (dp->ids.port_id == fc_host_port_id(lport->host))
  578. goto out;
  579. rdata = lport->tt.rport_create(lport, &dp->ids);
  580. if (rdata) {
  581. kfree(dp);
  582. lport->tt.rport_login(rdata);
  583. }
  584. return;
  585. out:
  586. kfree(dp);
  587. }
  588. /**
  589. * fc_disc_stop() - Stop discovery for a given lport
  590. * @lport: The lport that discovery should stop for
  591. */
  592. void fc_disc_stop(struct fc_lport *lport)
  593. {
  594. struct fc_disc *disc = &lport->disc;
  595. if (disc) {
  596. cancel_delayed_work_sync(&disc->disc_work);
  597. fc_disc_stop_rports(disc);
  598. }
  599. }
  600. /**
  601. * fc_disc_stop_final() - Stop discovery for a given lport
  602. * @lport: The lport that discovery should stop for
  603. *
  604. * This function will block until discovery has been
  605. * completely stopped and all rports have been deleted.
  606. */
  607. void fc_disc_stop_final(struct fc_lport *lport)
  608. {
  609. fc_disc_stop(lport);
  610. lport->tt.rport_flush_queue();
  611. }
  612. /**
  613. * fc_disc_init() - Initialize the discovery block
  614. * @lport: FC local port
  615. */
  616. int fc_disc_init(struct fc_lport *lport)
  617. {
  618. struct fc_disc *disc;
  619. if (!lport->tt.disc_start)
  620. lport->tt.disc_start = fc_disc_start;
  621. if (!lport->tt.disc_stop)
  622. lport->tt.disc_stop = fc_disc_stop;
  623. if (!lport->tt.disc_stop_final)
  624. lport->tt.disc_stop_final = fc_disc_stop_final;
  625. if (!lport->tt.disc_recv_req)
  626. lport->tt.disc_recv_req = fc_disc_recv_req;
  627. disc = &lport->disc;
  628. INIT_DELAYED_WORK(&disc->disc_work, fc_disc_timeout);
  629. mutex_init(&disc->disc_mutex);
  630. INIT_LIST_HEAD(&disc->rports);
  631. disc->lport = lport;
  632. return 0;
  633. }
  634. EXPORT_SYMBOL(fc_disc_init);