fc_disc.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734
  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/slab.h>
  34. #include <linux/err.h>
  35. #include <asm/unaligned.h>
  36. #include <scsi/fc/fc_gs.h>
  37. #include <scsi/libfc.h>
  38. #include "fc_libfc.h"
  39. #define FC_DISC_RETRY_LIMIT 3 /* max retries */
  40. #define FC_DISC_RETRY_DELAY 500UL /* (msecs) delay */
  41. static void fc_disc_gpn_ft_req(struct fc_disc *);
  42. static void fc_disc_gpn_ft_resp(struct fc_seq *, struct fc_frame *, void *);
  43. static void fc_disc_done(struct fc_disc *, enum fc_disc_event);
  44. static void fc_disc_timeout(struct work_struct *);
  45. static int fc_disc_single(struct fc_lport *, 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 remote ports 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;
  58. lport = fc_disc_lport(disc);
  59. mutex_lock(&disc->disc_mutex);
  60. list_for_each_entry_rcu(rdata, &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. * @disc: The discovery object to which the RSCN applies
  67. * @fp: The RSCN frame
  68. *
  69. * Locking Note: This function expects that the disc_mutex is locked
  70. * before it is called.
  71. */
  72. static void fc_disc_recv_rscn_req(struct fc_disc *disc, struct fc_frame *fp)
  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 = fc_disc_lport(disc);
  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. "(%6.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(fp, 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.reason = ELS_RJT_LOGIC;
  162. rjt_data.explan = ELS_EXPL_NONE;
  163. lport->tt.seq_els_rsp_send(fp, ELS_LS_RJT, &rjt_data);
  164. fc_frame_free(fp);
  165. }
  166. /**
  167. * fc_disc_recv_req() - Handle incoming requests
  168. * @lport: The local port receiving the request
  169. * @fp: The request frame
  170. *
  171. * Locking Note: This function is called from the EM and will lock
  172. * the disc_mutex before calling the handler for the
  173. * request.
  174. */
  175. static void fc_disc_recv_req(struct fc_lport *lport, struct fc_frame *fp)
  176. {
  177. u8 op;
  178. struct fc_disc *disc = &lport->disc;
  179. op = fc_frame_payload_op(fp);
  180. switch (op) {
  181. case ELS_RSCN:
  182. mutex_lock(&disc->disc_mutex);
  183. fc_disc_recv_rscn_req(disc, fp);
  184. mutex_unlock(&disc->disc_mutex);
  185. break;
  186. default:
  187. FC_DISC_DBG(disc, "Received an unsupported request, "
  188. "the opcode is (%x)\n", op);
  189. break;
  190. }
  191. }
  192. /**
  193. * fc_disc_restart() - Restart discovery
  194. * @disc: The discovery object to be restarted
  195. *
  196. * Locking Note: This function expects that the disc mutex
  197. * is already locked.
  198. */
  199. static void fc_disc_restart(struct fc_disc *disc)
  200. {
  201. if (!disc->disc_callback)
  202. return;
  203. FC_DISC_DBG(disc, "Restarting discovery\n");
  204. disc->requested = 1;
  205. if (disc->pending)
  206. return;
  207. /*
  208. * Advance disc_id. This is an arbitrary non-zero number that will
  209. * match the value in the fc_rport_priv after discovery for all
  210. * freshly-discovered remote ports. Avoid wrapping to zero.
  211. */
  212. disc->disc_id = (disc->disc_id + 2) | 1;
  213. disc->retry_count = 0;
  214. fc_disc_gpn_ft_req(disc);
  215. }
  216. /**
  217. * fc_disc_start() - Start discovery on a local port
  218. * @lport: The local port to have discovery started on
  219. * @disc_callback: Callback function to be called when discovery is complete
  220. */
  221. static void fc_disc_start(void (*disc_callback)(struct fc_lport *,
  222. enum fc_disc_event),
  223. struct fc_lport *lport)
  224. {
  225. struct fc_disc *disc = &lport->disc;
  226. /*
  227. * At this point we may have a new disc job or an existing
  228. * one. Either way, let's lock when we make changes to it
  229. * and send the GPN_FT request.
  230. */
  231. mutex_lock(&disc->disc_mutex);
  232. disc->disc_callback = disc_callback;
  233. fc_disc_restart(disc);
  234. mutex_unlock(&disc->disc_mutex);
  235. }
  236. /**
  237. * fc_disc_done() - Discovery has been completed
  238. * @disc: The discovery context
  239. * @event: The discovery completion status
  240. *
  241. * Locking Note: This function expects that the disc mutex is locked before
  242. * it is called. The discovery callback is then made with the lock released,
  243. * and the lock is re-taken before returning from this function
  244. */
  245. static void fc_disc_done(struct fc_disc *disc, enum fc_disc_event event)
  246. {
  247. struct fc_lport *lport = fc_disc_lport(disc);
  248. struct fc_rport_priv *rdata;
  249. FC_DISC_DBG(disc, "Discovery complete\n");
  250. disc->pending = 0;
  251. if (disc->requested) {
  252. fc_disc_restart(disc);
  253. return;
  254. }
  255. /*
  256. * Go through all remote ports. If they were found in the latest
  257. * discovery, reverify or log them in. Otherwise, log them out.
  258. * Skip ports which were never discovered. These are the dNS port
  259. * and ports which were created by PLOGI.
  260. */
  261. list_for_each_entry_rcu(rdata, &disc->rports, peers) {
  262. if (!rdata->disc_id)
  263. continue;
  264. if (rdata->disc_id == disc->disc_id)
  265. lport->tt.rport_login(rdata);
  266. else
  267. lport->tt.rport_logoff(rdata);
  268. }
  269. mutex_unlock(&disc->disc_mutex);
  270. disc->disc_callback(lport, event);
  271. mutex_lock(&disc->disc_mutex);
  272. }
  273. /**
  274. * fc_disc_error() - Handle error on dNS request
  275. * @disc: The discovery context
  276. * @fp: The error code encoded as a frame pointer
  277. */
  278. static void fc_disc_error(struct fc_disc *disc, struct fc_frame *fp)
  279. {
  280. struct fc_lport *lport = fc_disc_lport(disc);
  281. unsigned long delay = 0;
  282. FC_DISC_DBG(disc, "Error %ld, retries %d/%d\n",
  283. PTR_ERR(fp), disc->retry_count,
  284. FC_DISC_RETRY_LIMIT);
  285. if (!fp || PTR_ERR(fp) == -FC_EX_TIMEOUT) {
  286. /*
  287. * Memory allocation failure, or the exchange timed out,
  288. * retry after delay.
  289. */
  290. if (disc->retry_count < FC_DISC_RETRY_LIMIT) {
  291. /* go ahead and retry */
  292. if (!fp)
  293. delay = msecs_to_jiffies(FC_DISC_RETRY_DELAY);
  294. else {
  295. delay = msecs_to_jiffies(lport->e_d_tov);
  296. /* timeout faster first time */
  297. if (!disc->retry_count)
  298. delay /= 4;
  299. }
  300. disc->retry_count++;
  301. schedule_delayed_work(&disc->disc_work, delay);
  302. } else
  303. fc_disc_done(disc, DISC_EV_FAILED);
  304. }
  305. }
  306. /**
  307. * fc_disc_gpn_ft_req() - Send Get Port Names by FC-4 type (GPN_FT) request
  308. * @lport: The discovery context
  309. *
  310. * Locking Note: This function expects that the disc_mutex is locked
  311. * before it is called.
  312. */
  313. static void fc_disc_gpn_ft_req(struct fc_disc *disc)
  314. {
  315. struct fc_frame *fp;
  316. struct fc_lport *lport = fc_disc_lport(disc);
  317. WARN_ON(!fc_lport_test_ready(lport));
  318. disc->pending = 1;
  319. disc->requested = 0;
  320. disc->buf_len = 0;
  321. disc->seq_count = 0;
  322. fp = fc_frame_alloc(lport,
  323. sizeof(struct fc_ct_hdr) +
  324. sizeof(struct fc_ns_gid_ft));
  325. if (!fp)
  326. goto err;
  327. if (lport->tt.elsct_send(lport, 0, fp,
  328. FC_NS_GPN_FT,
  329. fc_disc_gpn_ft_resp,
  330. disc, 3 * lport->r_a_tov))
  331. return;
  332. err:
  333. fc_disc_error(disc, NULL);
  334. }
  335. /**
  336. * fc_disc_gpn_ft_parse() - Parse the body of the dNS GPN_FT response.
  337. * @lport: The local port the GPN_FT was received on
  338. * @buf: The GPN_FT response buffer
  339. * @len: The size of response buffer
  340. *
  341. * Goes through the list of IDs and names resulting from a request.
  342. */
  343. static int fc_disc_gpn_ft_parse(struct fc_disc *disc, void *buf, size_t len)
  344. {
  345. struct fc_lport *lport;
  346. struct fc_gpn_ft_resp *np;
  347. char *bp;
  348. size_t plen;
  349. size_t tlen;
  350. int error = 0;
  351. struct fc_rport_identifiers ids;
  352. struct fc_rport_priv *rdata;
  353. lport = fc_disc_lport(disc);
  354. disc->seq_count++;
  355. /*
  356. * Handle partial name record left over from previous call.
  357. */
  358. bp = buf;
  359. plen = len;
  360. np = (struct fc_gpn_ft_resp *)bp;
  361. tlen = disc->buf_len;
  362. disc->buf_len = 0;
  363. if (tlen) {
  364. WARN_ON(tlen >= sizeof(*np));
  365. plen = sizeof(*np) - tlen;
  366. WARN_ON(plen <= 0);
  367. WARN_ON(plen >= sizeof(*np));
  368. if (plen > len)
  369. plen = len;
  370. np = &disc->partial_buf;
  371. memcpy((char *)np + tlen, bp, plen);
  372. /*
  373. * Set bp so that the loop below will advance it to the
  374. * first valid full name element.
  375. */
  376. bp -= tlen;
  377. len += tlen;
  378. plen += tlen;
  379. disc->buf_len = (unsigned char) plen;
  380. if (plen == sizeof(*np))
  381. disc->buf_len = 0;
  382. }
  383. /*
  384. * Handle full name records, including the one filled from above.
  385. * Normally, np == bp and plen == len, but from the partial case above,
  386. * bp, len describe the overall buffer, and np, plen describe the
  387. * partial buffer, which if would usually be full now.
  388. * After the first time through the loop, things return to "normal".
  389. */
  390. while (plen >= sizeof(*np)) {
  391. ids.port_id = ntoh24(np->fp_fid);
  392. ids.port_name = ntohll(np->fp_wwpn);
  393. if (ids.port_id != lport->port_id &&
  394. ids.port_name != lport->wwpn) {
  395. rdata = lport->tt.rport_create(lport, ids.port_id);
  396. if (rdata) {
  397. rdata->ids.port_name = ids.port_name;
  398. rdata->disc_id = disc->disc_id;
  399. } else {
  400. printk(KERN_WARNING "libfc: Failed to allocate "
  401. "memory for the newly discovered port "
  402. "(%6.6x)\n", ids.port_id);
  403. error = -ENOMEM;
  404. }
  405. }
  406. if (np->fp_flags & FC_NS_FID_LAST) {
  407. fc_disc_done(disc, DISC_EV_SUCCESS);
  408. len = 0;
  409. break;
  410. }
  411. len -= sizeof(*np);
  412. bp += sizeof(*np);
  413. np = (struct fc_gpn_ft_resp *)bp;
  414. plen = len;
  415. }
  416. /*
  417. * Save any partial record at the end of the buffer for next time.
  418. */
  419. if (error == 0 && len > 0 && len < sizeof(*np)) {
  420. if (np != &disc->partial_buf) {
  421. FC_DISC_DBG(disc, "Partial buffer remains "
  422. "for discovery\n");
  423. memcpy(&disc->partial_buf, np, len);
  424. }
  425. disc->buf_len = (unsigned char) len;
  426. }
  427. return error;
  428. }
  429. /**
  430. * fc_disc_timeout() - Handler for discovery timeouts
  431. * @work: Structure holding discovery context that needs to retry discovery
  432. */
  433. static void fc_disc_timeout(struct work_struct *work)
  434. {
  435. struct fc_disc *disc = container_of(work,
  436. struct fc_disc,
  437. disc_work.work);
  438. mutex_lock(&disc->disc_mutex);
  439. fc_disc_gpn_ft_req(disc);
  440. mutex_unlock(&disc->disc_mutex);
  441. }
  442. /**
  443. * fc_disc_gpn_ft_resp() - Handle a response frame from Get Port Names (GPN_FT)
  444. * @sp: The sequence that the GPN_FT response was received on
  445. * @fp: The GPN_FT response frame
  446. * @lp_arg: The discovery context
  447. *
  448. * Locking Note: This function is called without disc mutex held, and
  449. * should do all its processing with the mutex held
  450. */
  451. static void fc_disc_gpn_ft_resp(struct fc_seq *sp, struct fc_frame *fp,
  452. void *disc_arg)
  453. {
  454. struct fc_disc *disc = disc_arg;
  455. struct fc_ct_hdr *cp;
  456. struct fc_frame_header *fh;
  457. enum fc_disc_event event = DISC_EV_NONE;
  458. unsigned int seq_cnt;
  459. unsigned int len;
  460. int error = 0;
  461. mutex_lock(&disc->disc_mutex);
  462. FC_DISC_DBG(disc, "Received a GPN_FT response\n");
  463. if (IS_ERR(fp)) {
  464. fc_disc_error(disc, fp);
  465. mutex_unlock(&disc->disc_mutex);
  466. return;
  467. }
  468. WARN_ON(!fc_frame_is_linear(fp)); /* buffer must be contiguous */
  469. fh = fc_frame_header_get(fp);
  470. len = fr_len(fp) - sizeof(*fh);
  471. seq_cnt = ntohs(fh->fh_seq_cnt);
  472. if (fr_sof(fp) == FC_SOF_I3 && seq_cnt == 0 && disc->seq_count == 0) {
  473. cp = fc_frame_payload_get(fp, sizeof(*cp));
  474. if (!cp) {
  475. FC_DISC_DBG(disc, "GPN_FT response too short, len %d\n",
  476. fr_len(fp));
  477. event = DISC_EV_FAILED;
  478. } else if (ntohs(cp->ct_cmd) == FC_FS_ACC) {
  479. /* Accepted, parse the response. */
  480. len -= sizeof(*cp);
  481. error = fc_disc_gpn_ft_parse(disc, cp + 1, len);
  482. } else if (ntohs(cp->ct_cmd) == FC_FS_RJT) {
  483. FC_DISC_DBG(disc, "GPN_FT rejected reason %x exp %x "
  484. "(check zoning)\n", cp->ct_reason,
  485. cp->ct_explan);
  486. event = DISC_EV_FAILED;
  487. if (cp->ct_reason == FC_FS_RJT_UNABL &&
  488. cp->ct_explan == FC_FS_EXP_FTNR)
  489. event = DISC_EV_SUCCESS;
  490. } else {
  491. FC_DISC_DBG(disc, "GPN_FT unexpected response code "
  492. "%x\n", ntohs(cp->ct_cmd));
  493. event = DISC_EV_FAILED;
  494. }
  495. } else if (fr_sof(fp) == FC_SOF_N3 && seq_cnt == disc->seq_count) {
  496. error = fc_disc_gpn_ft_parse(disc, fh + 1, len);
  497. } else {
  498. FC_DISC_DBG(disc, "GPN_FT unexpected frame - out of sequence? "
  499. "seq_cnt %x expected %x sof %x eof %x\n",
  500. seq_cnt, disc->seq_count, fr_sof(fp), fr_eof(fp));
  501. event = DISC_EV_FAILED;
  502. }
  503. if (error)
  504. fc_disc_error(disc, fp);
  505. else if (event != DISC_EV_NONE)
  506. fc_disc_done(disc, event);
  507. fc_frame_free(fp);
  508. mutex_unlock(&disc->disc_mutex);
  509. }
  510. /**
  511. * fc_disc_gpn_id_resp() - Handle a response frame from Get Port Names (GPN_ID)
  512. * @sp: The sequence the GPN_ID is on
  513. * @fp: The response frame
  514. * @rdata_arg: The remote port that sent the GPN_ID response
  515. *
  516. * Locking Note: This function is called without disc mutex held.
  517. */
  518. static void fc_disc_gpn_id_resp(struct fc_seq *sp, struct fc_frame *fp,
  519. void *rdata_arg)
  520. {
  521. struct fc_rport_priv *rdata = rdata_arg;
  522. struct fc_rport_priv *new_rdata;
  523. struct fc_lport *lport;
  524. struct fc_disc *disc;
  525. struct fc_ct_hdr *cp;
  526. struct fc_ns_gid_pn *pn;
  527. u64 port_name;
  528. lport = rdata->local_port;
  529. disc = &lport->disc;
  530. mutex_lock(&disc->disc_mutex);
  531. if (PTR_ERR(fp) == -FC_EX_CLOSED)
  532. goto out;
  533. if (IS_ERR(fp))
  534. goto redisc;
  535. cp = fc_frame_payload_get(fp, sizeof(*cp));
  536. if (!cp)
  537. goto redisc;
  538. if (ntohs(cp->ct_cmd) == FC_FS_ACC) {
  539. if (fr_len(fp) < sizeof(struct fc_frame_header) +
  540. sizeof(*cp) + sizeof(*pn))
  541. goto redisc;
  542. pn = (struct fc_ns_gid_pn *)(cp + 1);
  543. port_name = get_unaligned_be64(&pn->fn_wwpn);
  544. if (rdata->ids.port_name == -1)
  545. rdata->ids.port_name = port_name;
  546. else if (rdata->ids.port_name != port_name) {
  547. FC_DISC_DBG(disc, "GPN_ID accepted. WWPN changed. "
  548. "Port-id %6.6x wwpn %16.16llx\n",
  549. rdata->ids.port_id, port_name);
  550. lport->tt.rport_logoff(rdata);
  551. new_rdata = lport->tt.rport_create(lport,
  552. rdata->ids.port_id);
  553. if (new_rdata) {
  554. new_rdata->disc_id = disc->disc_id;
  555. lport->tt.rport_login(new_rdata);
  556. }
  557. goto out;
  558. }
  559. rdata->disc_id = disc->disc_id;
  560. lport->tt.rport_login(rdata);
  561. } else if (ntohs(cp->ct_cmd) == FC_FS_RJT) {
  562. FC_DISC_DBG(disc, "GPN_ID rejected reason %x exp %x\n",
  563. cp->ct_reason, cp->ct_explan);
  564. lport->tt.rport_logoff(rdata);
  565. } else {
  566. FC_DISC_DBG(disc, "GPN_ID unexpected response code %x\n",
  567. ntohs(cp->ct_cmd));
  568. redisc:
  569. fc_disc_restart(disc);
  570. }
  571. out:
  572. mutex_unlock(&disc->disc_mutex);
  573. kref_put(&rdata->kref, lport->tt.rport_destroy);
  574. }
  575. /**
  576. * fc_disc_gpn_id_req() - Send Get Port Names by ID (GPN_ID) request
  577. * @lport: The local port to initiate discovery on
  578. * @rdata: remote port private data
  579. *
  580. * Locking Note: This function expects that the disc_mutex is locked
  581. * before it is called.
  582. * On failure, an error code is returned.
  583. */
  584. static int fc_disc_gpn_id_req(struct fc_lport *lport,
  585. struct fc_rport_priv *rdata)
  586. {
  587. struct fc_frame *fp;
  588. fp = fc_frame_alloc(lport, sizeof(struct fc_ct_hdr) +
  589. sizeof(struct fc_ns_fid));
  590. if (!fp)
  591. return -ENOMEM;
  592. if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, FC_NS_GPN_ID,
  593. fc_disc_gpn_id_resp, rdata,
  594. 3 * lport->r_a_tov))
  595. return -ENOMEM;
  596. kref_get(&rdata->kref);
  597. return 0;
  598. }
  599. /**
  600. * fc_disc_single() - Discover the directory information for a single target
  601. * @lport: The local port the remote port is associated with
  602. * @dp: The port to rediscover
  603. *
  604. * Locking Note: This function expects that the disc_mutex is locked
  605. * before it is called.
  606. */
  607. static int fc_disc_single(struct fc_lport *lport, struct fc_disc_port *dp)
  608. {
  609. struct fc_rport_priv *rdata;
  610. rdata = lport->tt.rport_create(lport, dp->port_id);
  611. if (!rdata)
  612. return -ENOMEM;
  613. rdata->disc_id = 0;
  614. return fc_disc_gpn_id_req(lport, rdata);
  615. }
  616. /**
  617. * fc_disc_stop() - Stop discovery for a given lport
  618. * @lport: The local port that discovery should stop on
  619. */
  620. void fc_disc_stop(struct fc_lport *lport)
  621. {
  622. struct fc_disc *disc = &lport->disc;
  623. if (disc->pending)
  624. cancel_delayed_work_sync(&disc->disc_work);
  625. fc_disc_stop_rports(disc);
  626. }
  627. /**
  628. * fc_disc_stop_final() - Stop discovery for a given lport
  629. * @lport: The lport that discovery should stop on
  630. *
  631. * This function will block until discovery has been
  632. * completely stopped and all rports have been deleted.
  633. */
  634. void fc_disc_stop_final(struct fc_lport *lport)
  635. {
  636. fc_disc_stop(lport);
  637. lport->tt.rport_flush_queue();
  638. }
  639. /**
  640. * fc_disc_init() - Initialize the discovery layer for a local port
  641. * @lport: The local port that needs the discovery layer to be initialized
  642. */
  643. int fc_disc_init(struct fc_lport *lport)
  644. {
  645. struct fc_disc *disc;
  646. if (!lport->tt.disc_start)
  647. lport->tt.disc_start = fc_disc_start;
  648. if (!lport->tt.disc_stop)
  649. lport->tt.disc_stop = fc_disc_stop;
  650. if (!lport->tt.disc_stop_final)
  651. lport->tt.disc_stop_final = fc_disc_stop_final;
  652. if (!lport->tt.disc_recv_req)
  653. lport->tt.disc_recv_req = fc_disc_recv_req;
  654. disc = &lport->disc;
  655. INIT_DELAYED_WORK(&disc->disc_work, fc_disc_timeout);
  656. mutex_init(&disc->disc_mutex);
  657. INIT_LIST_HEAD(&disc->rports);
  658. disc->priv = lport;
  659. return 0;
  660. }
  661. EXPORT_SYMBOL(fc_disc_init);