fc_disc.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758
  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 <linux/export.h>
  36. #include <linux/rculist.h>
  37. #include <asm/unaligned.h>
  38. #include <scsi/fc/fc_gs.h>
  39. #include <scsi/libfc.h>
  40. #include "fc_libfc.h"
  41. #define FC_DISC_RETRY_LIMIT 3 /* max retries */
  42. #define FC_DISC_RETRY_DELAY 500UL /* (msecs) delay */
  43. static void fc_disc_gpn_ft_req(struct fc_disc *);
  44. static void fc_disc_gpn_ft_resp(struct fc_seq *, struct fc_frame *, void *);
  45. static void fc_disc_done(struct fc_disc *, enum fc_disc_event);
  46. static void fc_disc_timeout(struct work_struct *);
  47. static int fc_disc_single(struct fc_lport *, struct fc_disc_port *);
  48. static void fc_disc_restart(struct fc_disc *);
  49. /**
  50. * fc_disc_stop_rports() - Delete all the remote ports associated with the lport
  51. * @disc: The discovery job to stop remote ports on
  52. */
  53. static void fc_disc_stop_rports(struct fc_disc *disc)
  54. {
  55. struct fc_rport_priv *rdata;
  56. lockdep_assert_held(&disc->disc_mutex);
  57. list_for_each_entry(rdata, &disc->rports, peers) {
  58. if (kref_get_unless_zero(&rdata->kref)) {
  59. fc_rport_logoff(rdata);
  60. kref_put(&rdata->kref, fc_rport_destroy);
  61. }
  62. }
  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. static void fc_disc_recv_rscn_req(struct fc_disc *disc, struct fc_frame *fp)
  70. {
  71. struct fc_lport *lport;
  72. struct fc_els_rscn *rp;
  73. struct fc_els_rscn_page *pp;
  74. struct fc_seq_els_data rjt_data;
  75. unsigned int len;
  76. int redisc = 0;
  77. enum fc_els_rscn_ev_qual ev_qual;
  78. enum fc_els_rscn_addr_fmt fmt;
  79. LIST_HEAD(disc_ports);
  80. struct fc_disc_port *dp, *next;
  81. lockdep_assert_held(&disc->disc_mutex);
  82. lport = fc_disc_lport(disc);
  83. FC_DISC_DBG(disc, "Received an RSCN event\n");
  84. /* make sure the frame contains an RSCN message */
  85. rp = fc_frame_payload_get(fp, sizeof(*rp));
  86. if (!rp)
  87. goto reject;
  88. /* make sure the page length is as expected (4 bytes) */
  89. if (rp->rscn_page_len != sizeof(*pp))
  90. goto reject;
  91. /* get the RSCN payload length */
  92. len = ntohs(rp->rscn_plen);
  93. if (len < sizeof(*rp))
  94. goto reject;
  95. /* make sure the frame contains the expected payload */
  96. rp = fc_frame_payload_get(fp, len);
  97. if (!rp)
  98. goto reject;
  99. /* payload must be a multiple of the RSCN page size */
  100. len -= sizeof(*rp);
  101. if (len % sizeof(*pp))
  102. goto reject;
  103. for (pp = (void *)(rp + 1); len > 0; len -= sizeof(*pp), pp++) {
  104. ev_qual = pp->rscn_page_flags >> ELS_RSCN_EV_QUAL_BIT;
  105. ev_qual &= ELS_RSCN_EV_QUAL_MASK;
  106. fmt = pp->rscn_page_flags >> ELS_RSCN_ADDR_FMT_BIT;
  107. fmt &= ELS_RSCN_ADDR_FMT_MASK;
  108. /*
  109. * if we get an address format other than port
  110. * (area, domain, fabric), then do a full discovery
  111. */
  112. switch (fmt) {
  113. case ELS_ADDR_FMT_PORT:
  114. FC_DISC_DBG(disc, "Port address format for port "
  115. "(%6.6x)\n", ntoh24(pp->rscn_fid));
  116. dp = kzalloc(sizeof(*dp), GFP_KERNEL);
  117. if (!dp) {
  118. redisc = 1;
  119. break;
  120. }
  121. dp->lp = lport;
  122. dp->port_id = ntoh24(pp->rscn_fid);
  123. list_add_tail(&dp->peers, &disc_ports);
  124. break;
  125. case ELS_ADDR_FMT_AREA:
  126. case ELS_ADDR_FMT_DOM:
  127. case ELS_ADDR_FMT_FAB:
  128. default:
  129. FC_DISC_DBG(disc, "Address format is (%d)\n", fmt);
  130. redisc = 1;
  131. break;
  132. }
  133. }
  134. fc_seq_els_rsp_send(fp, ELS_LS_ACC, NULL);
  135. /*
  136. * If not doing a complete rediscovery, do GPN_ID on
  137. * the individual ports mentioned in the list.
  138. * If any of these get an error, do a full rediscovery.
  139. * In any case, go through the list and free the entries.
  140. */
  141. list_for_each_entry_safe(dp, next, &disc_ports, peers) {
  142. list_del(&dp->peers);
  143. if (!redisc)
  144. redisc = fc_disc_single(lport, dp);
  145. kfree(dp);
  146. }
  147. if (redisc) {
  148. FC_DISC_DBG(disc, "RSCN received: rediscovering\n");
  149. fc_disc_restart(disc);
  150. } else {
  151. FC_DISC_DBG(disc, "RSCN received: not rediscovering. "
  152. "redisc %d state %d in_prog %d\n",
  153. redisc, lport->state, disc->pending);
  154. }
  155. fc_frame_free(fp);
  156. return;
  157. reject:
  158. FC_DISC_DBG(disc, "Received a bad RSCN frame\n");
  159. rjt_data.reason = ELS_RJT_LOGIC;
  160. rjt_data.explan = ELS_EXPL_NONE;
  161. fc_seq_els_rsp_send(fp, ELS_LS_RJT, &rjt_data);
  162. fc_frame_free(fp);
  163. }
  164. /**
  165. * fc_disc_recv_req() - Handle incoming requests
  166. * @lport: The local port receiving the request
  167. * @fp: The request frame
  168. *
  169. * Locking Note: This function is called from the EM and will lock
  170. * the disc_mutex before calling the handler for the
  171. * request.
  172. */
  173. static void fc_disc_recv_req(struct fc_lport *lport, struct fc_frame *fp)
  174. {
  175. u8 op;
  176. struct fc_disc *disc = &lport->disc;
  177. op = fc_frame_payload_op(fp);
  178. switch (op) {
  179. case ELS_RSCN:
  180. mutex_lock(&disc->disc_mutex);
  181. fc_disc_recv_rscn_req(disc, fp);
  182. mutex_unlock(&disc->disc_mutex);
  183. break;
  184. default:
  185. FC_DISC_DBG(disc, "Received an unsupported request, "
  186. "the opcode is (%x)\n", op);
  187. fc_frame_free(fp);
  188. break;
  189. }
  190. }
  191. /**
  192. * fc_disc_restart() - Restart discovery
  193. * @disc: The discovery object to be restarted
  194. */
  195. static void fc_disc_restart(struct fc_disc *disc)
  196. {
  197. lockdep_assert_held(&disc->disc_mutex);
  198. if (!disc->disc_callback)
  199. return;
  200. FC_DISC_DBG(disc, "Restarting discovery\n");
  201. disc->requested = 1;
  202. if (disc->pending)
  203. return;
  204. /*
  205. * Advance disc_id. This is an arbitrary non-zero number that will
  206. * match the value in the fc_rport_priv after discovery for all
  207. * freshly-discovered remote ports. Avoid wrapping to zero.
  208. */
  209. disc->disc_id = (disc->disc_id + 2) | 1;
  210. disc->retry_count = 0;
  211. fc_disc_gpn_ft_req(disc);
  212. }
  213. /**
  214. * fc_disc_start() - Start discovery on a local port
  215. * @lport: The local port to have discovery started on
  216. * @disc_callback: Callback function to be called when discovery is complete
  217. */
  218. static void fc_disc_start(void (*disc_callback)(struct fc_lport *,
  219. enum fc_disc_event),
  220. struct fc_lport *lport)
  221. {
  222. struct fc_disc *disc = &lport->disc;
  223. /*
  224. * At this point we may have a new disc job or an existing
  225. * one. Either way, let's lock when we make changes to it
  226. * and send the GPN_FT request.
  227. */
  228. mutex_lock(&disc->disc_mutex);
  229. disc->disc_callback = disc_callback;
  230. fc_disc_restart(disc);
  231. mutex_unlock(&disc->disc_mutex);
  232. }
  233. /**
  234. * fc_disc_done() - Discovery has been completed
  235. * @disc: The discovery context
  236. * @event: The discovery completion status
  237. */
  238. static void fc_disc_done(struct fc_disc *disc, enum fc_disc_event event)
  239. {
  240. struct fc_lport *lport = fc_disc_lport(disc);
  241. struct fc_rport_priv *rdata;
  242. lockdep_assert_held(&disc->disc_mutex);
  243. FC_DISC_DBG(disc, "Discovery complete\n");
  244. disc->pending = 0;
  245. if (disc->requested) {
  246. fc_disc_restart(disc);
  247. return;
  248. }
  249. /*
  250. * Go through all remote ports. If they were found in the latest
  251. * discovery, reverify or log them in. Otherwise, log them out.
  252. * Skip ports which were never discovered. These are the dNS port
  253. * and ports which were created by PLOGI.
  254. *
  255. * We don't need to use the _rcu variant here as the rport list
  256. * is protected by the disc mutex which is already held on entry.
  257. */
  258. list_for_each_entry(rdata, &disc->rports, peers) {
  259. if (!kref_get_unless_zero(&rdata->kref))
  260. continue;
  261. if (rdata->disc_id) {
  262. if (rdata->disc_id == disc->disc_id)
  263. fc_rport_login(rdata);
  264. else
  265. fc_rport_logoff(rdata);
  266. }
  267. kref_put(&rdata->kref, fc_rport_destroy);
  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. } else if (PTR_ERR(fp) == -FC_EX_CLOSED) {
  305. /*
  306. * if discovery fails due to lport reset, clear
  307. * pending flag so that subsequent discovery can
  308. * continue
  309. */
  310. disc->pending = 0;
  311. }
  312. }
  313. /**
  314. * fc_disc_gpn_ft_req() - Send Get Port Names by FC-4 type (GPN_FT) request
  315. * @lport: The discovery context
  316. */
  317. static void fc_disc_gpn_ft_req(struct fc_disc *disc)
  318. {
  319. struct fc_frame *fp;
  320. struct fc_lport *lport = fc_disc_lport(disc);
  321. lockdep_assert_held(&disc->disc_mutex);
  322. WARN_ON(!fc_lport_test_ready(lport));
  323. disc->pending = 1;
  324. disc->requested = 0;
  325. disc->buf_len = 0;
  326. disc->seq_count = 0;
  327. fp = fc_frame_alloc(lport,
  328. sizeof(struct fc_ct_hdr) +
  329. sizeof(struct fc_ns_gid_ft));
  330. if (!fp)
  331. goto err;
  332. if (lport->tt.elsct_send(lport, 0, fp,
  333. FC_NS_GPN_FT,
  334. fc_disc_gpn_ft_resp,
  335. disc, 3 * lport->r_a_tov))
  336. return;
  337. err:
  338. fc_disc_error(disc, NULL);
  339. }
  340. /**
  341. * fc_disc_gpn_ft_parse() - Parse the body of the dNS GPN_FT response.
  342. * @lport: The local port the GPN_FT was received on
  343. * @buf: The GPN_FT response buffer
  344. * @len: The size of response buffer
  345. *
  346. * Goes through the list of IDs and names resulting from a request.
  347. */
  348. static int fc_disc_gpn_ft_parse(struct fc_disc *disc, void *buf, size_t len)
  349. {
  350. struct fc_lport *lport;
  351. struct fc_gpn_ft_resp *np;
  352. char *bp;
  353. size_t plen;
  354. size_t tlen;
  355. int error = 0;
  356. struct fc_rport_identifiers ids;
  357. struct fc_rport_priv *rdata;
  358. lport = fc_disc_lport(disc);
  359. disc->seq_count++;
  360. /*
  361. * Handle partial name record left over from previous call.
  362. */
  363. bp = buf;
  364. plen = len;
  365. np = (struct fc_gpn_ft_resp *)bp;
  366. tlen = disc->buf_len;
  367. disc->buf_len = 0;
  368. if (tlen) {
  369. WARN_ON(tlen >= sizeof(*np));
  370. plen = sizeof(*np) - tlen;
  371. WARN_ON(plen <= 0);
  372. WARN_ON(plen >= sizeof(*np));
  373. if (plen > len)
  374. plen = len;
  375. np = &disc->partial_buf;
  376. memcpy((char *)np + tlen, bp, plen);
  377. /*
  378. * Set bp so that the loop below will advance it to the
  379. * first valid full name element.
  380. */
  381. bp -= tlen;
  382. len += tlen;
  383. plen += tlen;
  384. disc->buf_len = (unsigned char) plen;
  385. if (plen == sizeof(*np))
  386. disc->buf_len = 0;
  387. }
  388. /*
  389. * Handle full name records, including the one filled from above.
  390. * Normally, np == bp and plen == len, but from the partial case above,
  391. * bp, len describe the overall buffer, and np, plen describe the
  392. * partial buffer, which if would usually be full now.
  393. * After the first time through the loop, things return to "normal".
  394. */
  395. while (plen >= sizeof(*np)) {
  396. ids.port_id = ntoh24(np->fp_fid);
  397. ids.port_name = ntohll(np->fp_wwpn);
  398. if (ids.port_id != lport->port_id &&
  399. ids.port_name != lport->wwpn) {
  400. rdata = fc_rport_create(lport, ids.port_id);
  401. if (rdata) {
  402. rdata->ids.port_name = ids.port_name;
  403. rdata->disc_id = disc->disc_id;
  404. } else {
  405. printk(KERN_WARNING "libfc: Failed to allocate "
  406. "memory for the newly discovered port "
  407. "(%6.6x)\n", ids.port_id);
  408. error = -ENOMEM;
  409. }
  410. }
  411. if (np->fp_flags & FC_NS_FID_LAST) {
  412. fc_disc_done(disc, DISC_EV_SUCCESS);
  413. len = 0;
  414. break;
  415. }
  416. len -= sizeof(*np);
  417. bp += sizeof(*np);
  418. np = (struct fc_gpn_ft_resp *)bp;
  419. plen = len;
  420. }
  421. /*
  422. * Save any partial record at the end of the buffer for next time.
  423. */
  424. if (error == 0 && len > 0 && len < sizeof(*np)) {
  425. if (np != &disc->partial_buf) {
  426. FC_DISC_DBG(disc, "Partial buffer remains "
  427. "for discovery\n");
  428. memcpy(&disc->partial_buf, np, len);
  429. }
  430. disc->buf_len = (unsigned char) len;
  431. }
  432. return error;
  433. }
  434. /**
  435. * fc_disc_timeout() - Handler for discovery timeouts
  436. * @work: Structure holding discovery context that needs to retry discovery
  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: The sequence that the GPN_FT response was received on
  450. * @fp: The GPN_FT response frame
  451. * @lp_arg: The discovery context
  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, ERR_PTR(error));
  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: The sequence the GPN_ID is on
  518. * @fp: The response frame
  519. * @rdata_arg: The remote port that sent the GPN_ID response
  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. if (PTR_ERR(fp) == -FC_EX_CLOSED)
  536. goto out;
  537. if (IS_ERR(fp))
  538. goto redisc;
  539. cp = fc_frame_payload_get(fp, sizeof(*cp));
  540. if (!cp)
  541. goto redisc;
  542. if (ntohs(cp->ct_cmd) == FC_FS_ACC) {
  543. if (fr_len(fp) < sizeof(struct fc_frame_header) +
  544. sizeof(*cp) + sizeof(*pn))
  545. goto redisc;
  546. pn = (struct fc_ns_gid_pn *)(cp + 1);
  547. port_name = get_unaligned_be64(&pn->fn_wwpn);
  548. mutex_lock(&rdata->rp_mutex);
  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 %6.6x wwpn %16.16llx\n",
  554. rdata->ids.port_id, port_name);
  555. mutex_unlock(&rdata->rp_mutex);
  556. fc_rport_logoff(rdata);
  557. mutex_lock(&lport->disc.disc_mutex);
  558. new_rdata = fc_rport_create(lport, rdata->ids.port_id);
  559. mutex_unlock(&lport->disc.disc_mutex);
  560. if (new_rdata) {
  561. new_rdata->disc_id = disc->disc_id;
  562. fc_rport_login(new_rdata);
  563. }
  564. goto out;
  565. }
  566. rdata->disc_id = disc->disc_id;
  567. mutex_unlock(&rdata->rp_mutex);
  568. fc_rport_login(rdata);
  569. } else if (ntohs(cp->ct_cmd) == FC_FS_RJT) {
  570. FC_DISC_DBG(disc, "GPN_ID rejected reason %x exp %x\n",
  571. cp->ct_reason, cp->ct_explan);
  572. fc_rport_logoff(rdata);
  573. } else {
  574. FC_DISC_DBG(disc, "GPN_ID unexpected response code %x\n",
  575. ntohs(cp->ct_cmd));
  576. redisc:
  577. mutex_lock(&disc->disc_mutex);
  578. fc_disc_restart(disc);
  579. mutex_unlock(&disc->disc_mutex);
  580. }
  581. out:
  582. kref_put(&rdata->kref, fc_rport_destroy);
  583. if (!IS_ERR(fp))
  584. fc_frame_free(fp);
  585. }
  586. /**
  587. * fc_disc_gpn_id_req() - Send Get Port Names by ID (GPN_ID) request
  588. * @lport: The local port to initiate discovery on
  589. * @rdata: remote port private data
  590. *
  591. * On failure, an error code is returned.
  592. */
  593. static int fc_disc_gpn_id_req(struct fc_lport *lport,
  594. struct fc_rport_priv *rdata)
  595. {
  596. struct fc_frame *fp;
  597. lockdep_assert_held(&lport->disc.disc_mutex);
  598. fp = fc_frame_alloc(lport, sizeof(struct fc_ct_hdr) +
  599. sizeof(struct fc_ns_fid));
  600. if (!fp)
  601. return -ENOMEM;
  602. if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, FC_NS_GPN_ID,
  603. fc_disc_gpn_id_resp, rdata,
  604. 3 * lport->r_a_tov))
  605. return -ENOMEM;
  606. kref_get(&rdata->kref);
  607. return 0;
  608. }
  609. /**
  610. * fc_disc_single() - Discover the directory information for a single target
  611. * @lport: The local port the remote port is associated with
  612. * @dp: The port to rediscover
  613. */
  614. static int fc_disc_single(struct fc_lport *lport, struct fc_disc_port *dp)
  615. {
  616. struct fc_rport_priv *rdata;
  617. lockdep_assert_held(&lport->disc.disc_mutex);
  618. rdata = fc_rport_create(lport, dp->port_id);
  619. if (!rdata)
  620. return -ENOMEM;
  621. rdata->disc_id = 0;
  622. return fc_disc_gpn_id_req(lport, rdata);
  623. }
  624. /**
  625. * fc_disc_stop() - Stop discovery for a given lport
  626. * @lport: The local port that discovery should stop on
  627. */
  628. static void fc_disc_stop(struct fc_lport *lport)
  629. {
  630. struct fc_disc *disc = &lport->disc;
  631. if (disc->pending)
  632. cancel_delayed_work_sync(&disc->disc_work);
  633. mutex_lock(&disc->disc_mutex);
  634. fc_disc_stop_rports(disc);
  635. mutex_unlock(&disc->disc_mutex);
  636. }
  637. /**
  638. * fc_disc_stop_final() - Stop discovery for a given lport
  639. * @lport: The lport that discovery should stop on
  640. *
  641. * This function will block until discovery has been
  642. * completely stopped and all rports have been deleted.
  643. */
  644. static void fc_disc_stop_final(struct fc_lport *lport)
  645. {
  646. fc_disc_stop(lport);
  647. fc_rport_flush_queue();
  648. }
  649. /**
  650. * fc_disc_config() - Configure the discovery layer for a local port
  651. * @lport: The local port that needs the discovery layer to be configured
  652. * @priv: Private data structre for users of the discovery layer
  653. */
  654. void fc_disc_config(struct fc_lport *lport, void *priv)
  655. {
  656. struct fc_disc *disc;
  657. if (!lport->tt.disc_start)
  658. lport->tt.disc_start = fc_disc_start;
  659. if (!lport->tt.disc_stop)
  660. lport->tt.disc_stop = fc_disc_stop;
  661. if (!lport->tt.disc_stop_final)
  662. lport->tt.disc_stop_final = fc_disc_stop_final;
  663. if (!lport->tt.disc_recv_req)
  664. lport->tt.disc_recv_req = fc_disc_recv_req;
  665. disc = &lport->disc;
  666. disc->priv = priv;
  667. }
  668. EXPORT_SYMBOL(fc_disc_config);
  669. /**
  670. * fc_disc_init() - Initialize the discovery layer for a local port
  671. * @lport: The local port that needs the discovery layer to be initialized
  672. */
  673. void fc_disc_init(struct fc_lport *lport)
  674. {
  675. struct fc_disc *disc = &lport->disc;
  676. INIT_DELAYED_WORK(&disc->disc_work, fc_disc_timeout);
  677. mutex_init(&disc->disc_mutex);
  678. INIT_LIST_HEAD(&disc->rports);
  679. }
  680. EXPORT_SYMBOL(fc_disc_init);