fc_disc.c 19 KB

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