fc_disc.c 19 KB

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