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