fc_disc.c 20 KB

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